Docker - 使用 SystemD 和 "daemon.json" 启用远程 HTTP API

Docker - Enable Remote HTTP API with SystemD and "daemon.json"

免责声明:

在使用 Ubuntu 14.04 和 Upstart 作为初始化系统的旧机器上,我通过在 /etc/default/docker 上定义 DOCKER_OPTS 启用了 HTTP API。有效。

$ docker version
Client:
 Version:      1.11.2
 (...)

Server:
 Version:      1.11.2
 (...)

问题:

此解决方案不适用于 Ubuntu 16.04 和 SystemD 的最新机器。

如最近安装的文件顶部所述/etc/default/docker:

# Docker Upstart and SysVinit configuration file

#
# THIS FILE DOES NOT APPLY TO SYSTEMD
#
#   Please see the documentation for "systemd drop-ins":
#   https://docs.docker.com/engine/articles/systemd/
#
(...)

当我在 Docker documentation page for SystemD I need to fill a daemon.json file but as stated on the reference 上查看此信息时,有些属性不言自明,但其他属性可能解释不足。

话虽这么说,我正在寻求帮助来转换它:

DOCKER_OPTS="-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock -G myuser --debug"

daemon.json对象?


注释

PS1: 我知道 daemon.json 默认有一个 debug: true

PS2: 可能 group: "myuser" 它会像这样工作或与字符串数组一起工作。

PS3: 我主要关心的是同时使用 SOCK 和 HTTP。


编辑 (8/08/2017) 阅读已接受的答案后,请查看@white_gecko 答案以获取有关该问题的更多信息。

有很多零散的文档很难解决这个问题。

我的第一个解决方案是用

创建 daemon.json
{
  "hosts": [
    "unix:///var/run/docker.sock",
    "tcp://127.0.0.1:2376"
  ]
}

在尝试使用 service docker restart 重新启动守护程序后,此错误 docker[5586]: unable to configure the Docker daemon with file /etc/docker/daemon.json 不起作用。 注意:还有更多关于我复制失败的错误。

但是这个错误在守护进程启动时意味着什么,它与 daemon.json 上的标志和配置冲突。

当我用 service docker status 查看它时,这是父进程:ExecStart=/usr/bin/docker daemon -H fd://

这很奇怪,因为与 /etc/init.d/docker 上的配置不同,我认为那是服务配置。 奇怪的是 init.d 上的文件确实包含对 daemon 参数的任何引用,既不包含 -H fd://.

经过一些研究和系统目录的大量搜索,我找到了这些目录(帮助讨论这个问题docker github issue #22339)。

解决方案

用这个新值从 /lib/systemd/system/docker.service 编辑了 ExecStart/usr/bin/docker daemon

并用

创建了/etc/docker/daemon.json
{
  "hosts": [
    "fd://",
    "tcp://127.0.0.1:2376"
  ]
}

终于用 service docker start 重新启动了服务,现在我在 service docker status 上得到了 "green light"。

测试了新配置:

$ docker run hello-world

Hello from Docker!
(...)

并且,

$ curl http://127.0.0.1:2376/v1.23/info
[JSON]

我希望这能帮助和我有类似问题的人! :)

我遇到了同样的问题,实际上在我看来,最简单的解决方案是使用 systemd 插件: 只需创建一个文件 /etc/systemd/system/docker.service 即可覆盖 /lib/systemd/system/docker.service.

中服务的特定部分

在这种情况下,/etc/systemd/system/docker.service 的内容将是:

[Service]
ExecStart=/usr/bin/dockerd --tlsverify --tlscacert=/etc/docker/ca.pem --tlscert=/etc/docker/server-cert.pem --tlskey=/etc/docker/server-key.pem -H=tcp://127.0.0.1:2375 -H=fd://

(您甚至可以创建一个包含多个文件的目录 docker.service.d 来覆盖不同的参数。)

添加文件后,您只需 运行:

$ sudo systemctl daemon-reload
$ sudo systemctl restart docker

https://docs.docker.com/engine/admin/#troubleshoot-conflicts-between-the-daemonjson-and-startup-scripts 中描述的解决方案适用于我:

One notable example of a configuration conflict that is difficult to troubleshoot is when you want to specify a different daemon address from the default. Docker listens on a socket by default. On Debian and Ubuntu systems using systemd), this means that a -H flag is always used when starting dockerd. If you specify a hosts entry in the daemon.json, this causes a configuration conflict (as in the above message) and Docker fails to start.

To work around this problem, create a new file /etc/systemd/system/docker.service.d/docker.conf with the following contents, to remove the -H argument that is used when starting the daemon by default.

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd

请注意,ExecStart= 行实际上是必需的,否则将失败并显示错误:

docker.service: Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.

创建文件后您必须 运行:

sudo systemctl daemon-reload
sudo systemctl restart docker

我在 Ubuntu 18.04.1 LTS 和 Docker 18.06.0-ce 创建 /etc/systemd/system/docker.service.d/remote-api.conf 内容如下:

[Service]
ExecStart=
ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2376 -H unix:///var/run/docker.sock

然后 运行 sudo systemctl daemon-reloadsudo systemctl restart docker 查看调用结果:

卷曲http://localhost:2376/info

如果您的 docker 在代理后面,您可能需要配置代理。 要在 /etc/default/docker 文件中实现此粘贴,请执行以下操作:

http_proxy="http://85.22.53.71:8080/"
https_proxy="http://85.22.53.71:8080/"
HTTP_PROXY="http://85.22.53.71:8080/"
HTTPS_PROXY="http://85.22.53.71:8080/"
# below you can list some *.enterprise_domain.com as well
NO_PROXY="localhost,127.0.0.1,::1" 

或创建 /etc/systemd/system/docker.service.d/remote-api.conf 内容如下:

[Service]
Environment="HTTP_PROXY=http://<you_proxy_ip>:<port>"
Environment="HTTPS_PROXY=https://<you_proxy_ip>:<port>/"
Environment="NO_PROXY=localhost,127.0.0.1,::1"

我希望它对某人有所帮助...