1. 程式人生 > >Docker學習筆記(四)-fixbug1-daemon.json

Docker學習筆記(四)-fixbug1-daemon.json

因daemon.json中DNS Server設定而引起的錯誤

在使用docker的過程中如果報出以下錯誤:

(1)無法連線到docker daemon (docker 守護程序)

docker: Cannot connect to the Docker daemon. Is the docker daemon running on this host?.
See 'docker run --help'.

(2) 重啟docker服務失敗

mingchen@mingchen-HP:~$ sudo service docker restart
docker stop/waiting
start:
Job failed to start

(3)在容器內部無法ping通百度、谷歌等外網

unknown host www.baidu.com

如何解決:

主要分為三個步驟。首先查詢主機使用的DNS伺服器;接著根據查詢結果設定 /etc/docker/daemon.json 中的DNS server, 這個daemon.json檔案實際上是docker中的daemon程序(守護程序)的配置檔案,而守護程序又是所有啟動的容器的父程序;最後,重啟docker 服務。

(1)查詢本機的DNS Server

可以通過以下命令之一:

$ nm-tool | grep DNS

或者

$ 
nmcli dev show | grep DNS

我的結果如下:

mingchen@mingchen-HP:/$ nm-tool |grep DNS
    DNS:             127.0.0.1
    DNS:             8.8.8.8
    DNS:             172.20.1.4
    DNS:             172.20.1.6

(2) 設定/etc/docker/daemon.json檔案

以sudo的許可權開啟,使用編輯器gedit, nano, vim ,vi 等,看讀者愛好了。

{
 "dns": ["8.8.8.8"
,"172.20.1.4","172.20.1.6", "127.0.0.1","8.8.4.4"]
}

按照上面的格式設定好。

(3) 重啟docker 服務

在終端輸入命令sudo service docker restart

mingchen@mingchen-HP:/$ sudo service docker restart
docker stop/waiting
docker start/running, process 1046

另外,我也曾經在docker開發者論壇上發過與這個問題相關的帖子,有興趣的話,可以去看看,完整回答在最底下,連結

N.B. 有關daemon.json檔案

以下是完整的daemon.json檔案可配置的引數表,我們在配置的過程中,只需要設定我們需要的引數即可,不必全部寫出來。詳細參考,官方文件

{
    "api-cors-header": "",
    "authorization-plugins": [],
    "bip": "",
    "bridge": "",
    "cgroup-parent": "",
    "cluster-store": "",
    "cluster-store-opts": {},
    "cluster-advertise": "",
    "debug": true,
    "default-gateway": "",
    "default-gateway-v6": "",
    "default-runtime": "runc",
    "default-ulimits": {},
    "disable-legacy-registry": false,
    "dns": [],
    "dns-opts": [],
    "dns-search": [],
    "exec-opts": [],
    "exec-root": "",
    "fixed-cidr": "",
    "fixed-cidr-v6": "",
    "graph": "",
    "group": "",
    "hosts": [],
    "icc": false,
    "insecure-registries": [],
    "ip": "0.0.0.0",
    "iptables": false,
    "ipv6": false,
    "ip-forward": false,
    "ip-masq": false,
    "labels": [],
    "live-restore": true,
    "log-driver": "",
    "log-level": "",
    "log-opts": {},
    "max-concurrent-downloads": 3,
    "max-concurrent-uploads": 5,
    "mtu": 0,
    "oom-score-adjust": -500,
    "pidfile": "",
    "raw-logs": false,
    "registry-mirrors": [],
    "runtimes": {
        "runc": {
            "path": "runc"
        },
        "custom": {
            "path": "/usr/local/bin/my-runc-replacement",
            "runtimeArgs": [
                "--debug"
            ]
        }
    },
    "selinux-enabled": false,
    "storage-driver": "",
    "storage-opts": [],
    "swarm-default-advertise-addr": "",
    "tls": true,
    "tlscacert": "",
    "tlscert": "",
    "tlskey": "",
    "tlsverify": true,
    "userland-proxy": false,
    "userns-remap": ""
}