将DOCKER_OPTS传递给Daemon.json.j2
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将DOCKER_OPTS传递给Daemon.json.j2相关的知识,希望对你有一定的参考价值。
我是ansible和docker的新手,我如何使用文件daemon.json.j2将DOCKER_OPTS =“ - H tcp://0.0.0.0:2375”传递给ansible中的/etc/init.d/docker
文件结构:templates - > daemon.json.j2
daemon.json.j2
{
{% if docker_use_dnsmasq %}
"dns": ["172.17.0.1"],
{% endif %}
{% if docker_use_tcp %}
"hosts": ["fd://", "tcp://0.0.0.0:2375"],
{% endif %}
"storage-driver": "{{ docker_driver }}",
"log-driver": "journald"
}
ansible:
- name: Add Docker config
template:
src: daemon.json.j2
dest: /etc/docker/daemon.json
owner: root
group: root
mode: 0644
when: 'docker_is_dockerized == false'
- name: Change the value
lineinfile:
path: /lib/systemd/system/docker.service
regexp: 'ExecStart=/usr/bin/dockerd -H fd://'
line: 'ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375'
when: 'docker_is_dockerized == false'
docker.service
● docker.service - Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: inactive (dead) (Result: exit-code) since Sat 2018-03-03 21:13:41 UTC; 3min 25s ago
Docs: https://docs.docker.com
Process: 1747 ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375 (code=exited, status=1/FAILURE)
Main PID: 1747 (code=exited, status=1/FAILURE)
Mar 03 21:13:41 ip-10-150-4-45 systemd[1]: docker.service: Main process exited, code=exited, status=1/FAILURE
Mar 03 21:13:41 ip-10-150-4-45 systemd[1]: Failed to start Docker Application Container Engine.
Mar 03 21:13:41 ip-10-150-4-45 systemd[1]: docker.service: Unit entered failed state.
Mar 03 21:13:41 ip-10-150-4-45 systemd[1]: docker.service: Failed with result 'exit-code'.
Mar 03 21:13:41 ip-10-150-4-45 systemd[1]: docker.service: Service hold-off time over, scheduling restart.
Mar 03 21:13:41 ip-10-150-4-45 systemd[1]: Stopped Docker Application Container Engine.
Mar 03 21:13:41 ip-10-150-4-45 systemd[1]: docker.service: Start request repeated too quickly.
Mar 03 21:13:41 ip-10-150-4-45 systemd[1]: Failed to start Docker Application Container Engine.
答案
如果要使用daemon.json修改主机字段,则需要设置systemd覆盖以删除在命令行上传递的主机选项。使用您的ansible脚本,这将是以下任务:
- name: 'Update systemd unit overrides for Docker'
copy:
content: |
[Service]
# next two lines needed to allow /etc/docker/daemon.json to adjust hosts
ExecStart=
ExecStart=/usr/bin/dockerd
dest: /etc/systemd/system/docker.service.d/override.conf
force: yes
owner: root
mode: 0644
对于那些没有ansible的人,上面创建了一个名为/etc/systemd/system/docker.service.d/override.conf
的文件,其中包含:
[Service]
# next two lines needed to allow /etc/docker/daemon.json to adjust hosts
ExecStart=
ExecStart=/usr/bin/dockerd
创建该文件后,您需要systemctl daemon-reload
。要重置故障限制,您需要systemctl reset-failed
。最后,要启动docker,你有systemctl start docker
。
以上是关于将DOCKER_OPTS传递给Daemon.json.j2的主要内容,如果未能解决你的问题,请参考以下文章