docker安裝后默認沒有daemon.json這個配置文件,需要進行手動創建。配置文件的默認路徑:/etc/docker/daemon.json
一般情況,配置文件 daemon.json中配置的項目參數,在啟動參數中同樣適用,有些可能不一樣(具體可以查看官方文檔),但需要注意的一點,配置文件中如果已經有某個配置項,則無法在啟動參數中增加,會出現沖突的錯誤。
如果在daemon.json文件中進行配置,需要docker版本高於1.12.6(在這個版本上不生效,1.13.1以上是生效的)
參數
daemon.json文件可配置的參數表,我們在配置的過程中,只需要設置我們需要的參數即可,不必全部寫出來。詳細參考官網。
官方的配置地址:https://docs.docker.com/engine/reference/commandline/dockerd/#/configuration-reloading。
官方的配置地址:https://docs.docker.com/engine/reference/commandline/dockerd/#options
官方的配置地址:https://docs.docker.com/engine/reference/commandline/dockerd/#/linux-configuration-file
{ "api-cors-header":"", "authorization-plugins":[], "bip": "", "bridge":"", "cgroup-parent":"", "cluster-store":"", "cluster-store-opts":{}, "cluster-advertise":"", "debug": true, #啟用debug的模式,啟用后,可以看到很多的啟動信息。默認false "default-gateway":"", "default-gateway-v6":"", "default-runtime":"runc", "default-ulimits":{}, "disable-legacy-registry":false, "dns": ["192.168.1.1"], # 設定容器DNS的地址,在容器的 /etc/resolv.conf文件中可查看。 "dns-opts": [], # 容器 /etc/resolv.conf 文件,其他設置 "dns-search": [], # 設定容器的搜索域,當設定搜索域為 .example.com 時,在搜索一個名為 host 的 主機時,DNS不僅搜索host,還會搜 索host.example.com 。 注意:如果不設置, Docker 會默認用主機上的 /etc/resolv.conf 來配置容器。 "exec-opts": [], "exec-root":"", "fixed-cidr":"", "fixed-cidr-v6":"", "graph":"/var/lib/docker", #已廢棄,使用data-root代替,這個主要看docker的版本 "data-root":"/var/lib/docker", #Docker運行時使用的根路徑,根路徑下的內容稍后介紹,默認/var/lib/docker "group": "", #Unix套接字的屬組,僅指/var/run/docker.sock "hosts": [], #設置容器hosts "icc": false, "insecure-registries": [], #配置docker的私庫地址 "ip":"0.0.0.0", "iptables": false, "ipv6": false, "ip-forward": false, #默認true, 啟用 net.ipv4.ip_forward ,進入容器后使用 sysctl -a | grepnet.ipv4.ip_forward 查看 "ip-masq":false, "labels":["nodeName=node-121"], # docker主機的標簽,很實用的功能,例如定義:–label nodeName=host-121 "live-restore": true, "log-driver":"", "log-level":"", "log-opts": {}, "max-concurrent-downloads":3, "max-concurrent-uploads":5, "mtu": 0, "oom-score-adjust":-500, "pidfile": "", #Docker守護進程的PID文件 "raw-logs": false, "registry-mirrors":["xxxx"], #鏡像加速的地址,增加后在 docker info中可查看。 "runtimes": { "runc": { "path": "runc" }, "custom": { "path":"/usr/local/bin/my-runc-replacement", "runtimeArgs": [ "--debug" ] } }, "selinux-enabled": false, #默認 false,啟用selinux支持 "storage-driver":"", "storage-opts": [], "swarm-default-advertise-addr":"", "tls": true, #默認 false, 啟動TLS認證開關 "tlscacert": "", #默認 ~/.docker/ca.pem,通過CA認證過的的certificate文件路徑 "tlscert": "", #默認 ~/.docker/cert.pem ,TLS的certificate文件路徑 "tlskey": "", #默認~/.docker/key.pem,TLS的key文件路徑 "tlsverify": true, #默認false,使用TLS並做后台進程與客戶端通訊的驗證 "userland-proxy":false, "userns-remap":"" }
上述是官網docs提供的一個示例配置,我們可以參考,選擇性的配置其中的部分內容。
示例
1、如何配置 registry 私庫相關的參數
涉及以下2個參數:
"insecure-registries": [], #這個私庫的服務地址 "registry-mirrors": [], #私庫加速器
2.配置示例:
# cat /etc/docker/daemon.json { "registry-mirrors": [ "https://d8b3zdiw.mirror.aliyuncs.com" ], "insecure-registries": [ "https://ower.site.com" ], }
配置與應用
1.默認沒有文件,所以我們需要先創建,進入/etc/docker目錄下,記得創建的文件所有者是root(vim或touch,記得chown修改所有者)
-rw-r--r-- 1 root root 71 Dec 19 17:25daemon.json
2.在文檔中配置想要添加的參數:如,鏡像加速器網站,私庫網站
# cat /etc/docker/daemon.json { "registry-mirrors":[ "https://d8b3zdiw.mirror.aliyuncs.com" ], "insecure-registries": [ "https://ower.site.com" ], }
3.創建並修改完daemon.json文件后,需要讓這個文件生效
a.修改完成后reload配置文件
sudo systemctl daemon-reload
b.重啟docker服務
sudo systemctl restart docker.service
c.查看狀態
sudo systemctl status docker -l
d.查看服務
sudo docker info
當我們需要對docker服務進行調整配置時,不用去修改主文件 docker.service的參數,通過daemon.json配置文件來管理,更為安全、合理。