Docker 配置允许远程访问
技术 Docker 4

方式1:修改systemd 配置

  1. 使用systemctl 修改启动配置
    systemctl edit docker.service
    
  2. 添加或编辑以下内容:
    [Service]
    ExecStart=
    ExecStart=/usr/bin/dockerd -H fd:// -H tcp://0.0.0.0:2375
    
  3. 刷新配置 重启docker
    systemctl daemon-reload
    systemcrl restart docker.service
    
  4. 验证
    netstat -lntp | grep dockerd

方式2:修改daemon.json 文件

  1. 文件路径:/etc/docker/daemon.json
  2. 编辑文件,加入以下内容:
    {
      "hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"]
    }
    
  3. 重启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 the daemon.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

Docker 配置允许远程访问
https://halo.persipa.site/2023/1012
作者
Persipa
发布于
更新于
许可