Docker 配置允许远程访问
方式1:修改systemd 配置
- 使用
systemctl
修改启动配置systemctl edit docker.service
- 添加或编辑以下内容:
[Service] ExecStart= ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375
- 刷新配置 重启docker
systemctl daemon-reload systemcrl restart docker.service
- 验证
netstat -lntp | grep dockerd
方式2:修改daemon.json
文件
- 文件路径:
/etc/docker/daemon.json
- 编辑文件,加入以下内容:
{ "hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"] }
- 重启docker
systemctl restart docker.service
注意:方式1 和方式2 不可同时使用,否则会无法启动
docker 官方是这么说的:
systemd vs
daemon.json
Configuring Docker to listen for connections using both the systemd unit file and thedaemon.json
file causes a conflict that prevents Docker from starting.
我选择了方式2 来允许远程访问,但是发现docker 启动不了了,检查docker.service 的systemd 文件,发现其中有内容如下:
[Service]
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
那么明显的systemd 的配置和daemon.json
的配置冲突了。
所以可以在/etc/systemd/system/docker.service.d/
文件夹增加一个配置文件override.conf
来覆盖掉默认的配置
# 把原本的ExecStart 的配置中的`-H fd://`删掉,其他不变
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --containerd=/run/containerd/containerd.sock
然后再根据方式2 的方法修改
参考文档:
Configure remote access for Docker daemon
Unable to start docker after configuring hosts in daemon.json