将DOCKER_OPTS传递给Daemon.json.j2

时间:2018-03-02 21:23:48

标签: docker ansible

我是ansible&的新手docker,我怎样才能传递DOCKER_OPTS =" -H tcp://0.0.0.0:2375"在使用文件daemon.json.j2的ansible中的/etc/init.d/docker

文件结构: 模板    - > 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.

1 个答案:

答案 0 :(得分:1)

如果要使用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