0%

在Ubuntu 16.04上开启Docker的Remote API

由于自动化的考虑,需要用docker的remote API,尝试了多种方法,最终才找到了可行的方法

可行的方法

  • 编辑/lib/systemd/system/docker.service
1
$ vim /lib/systemd/system/docker.service
  • 修改ExecStart的参数
1
ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375
  • 随后执行service docker restart时会提示Warning: docker.service changed on disk. Run 'systemctl daemon-reload' to reload units.则表示配置成功
1
systemctl daemon-reload
  • 重启docker服务
1
service docker restart
  • 测试是否成功
1
2
$ curl http://localhost:2375/containers/json
[{"Id":"30c8e35f1292421d11f6b09385a4fc980d6abaca591d0f52b18dbad8e4f5be04","Names":["/clever_murdock"],"Image":"portainer/portainer","ImageID":"sha256:a8f2aeb34cf69178be1d152759fb17ccff7915faf750c82cd7d1851b12ec7b37","Command":"/portainer","Created":1520845664,"Ports":[{"IP":"0.0.0.0","PrivatePort":9000,"PublicPort":9000,"Type":"tcp"}],"Labels":{},"State":"running","Status":"Up 13 minutes","HostConfig":{"NetworkMode":"default"},"NetworkSettings":{"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"NetworkID":"78fa057306e70838bab1e18359c86bd8eff7de2285c351784ad951cd7a73f8d1","EndpointID":"e99ca98169320155c8833a8746be7d0e1c8d98186c75fba9d9bf2486367a4e00","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"MacAddress":"02:42:ac:11:00:02","DriverOpts":null}}},"Mounts":[{"Type":"bind","Source":"/opt/portainer","Destination":"/data","Mode":"","RW":true,"Propagation":"rprivate"},{"Type":"bind","Source":"/var/run/docker.sock","Destination":"/var/run/docker.sock","Mode":"","RW":true,"Propagation":"rprivate"}]}]

不可行的方法

同时列出在Ubuntu上不可行的方法

  • 修改/etc/default/docker中的DOCKER_OPTS
1
DOCKER_OPTS='-H fd:// -H tcp://0.0.0.0:2375'
  • 修改/etc/init/docker.conf中的DOCKER_OPTS

网上有人说在Ubuntu14.04上可以生效

1
2
3
# modify these in /etc/default/$UPSTART_JOB (/etc/default/docker)
DOCKERD=/usr/bin/dockerd
DOCKER_OPTS='-H unix:///var/run/docker.sock -H tcp://0.0.0.0:2375'