Docker配置远程连接

前言

默认 Docker 是通过非网络的 Unix 套接字运行的,只能够进行本地通信(/var/run/docker.sock),是不能够直接远程连接 Docker 的。

开始配置

编辑 /etc/docker/daemon.json 文件

复制代码
{
  "hosts": ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"]
}

重启 Docker 服务

复制代码
sudo systemctl restart docker

检测 Docker 服务

复制代码
systemctl status docker

解决问题

复制代码
unable to configure the Docker daemon with file /etc/docker/daemon.json: the following directives are specified both as a flag and in the configuration file: hosts: (from flag: [fd://], from file: [unix:///>

Docker 守护进程的配置冲突:hosts 参数在 /etc/docker/daemon.json 文件中和 systemd 服务的启动参数(flag)中同时被定义,导致 Docker 无法启动。具体的冲突是:

  • daemon.json 配置了 hosts: ["unix:///var/run/docker.sock", "tcp://0.0.0.0:2375"]。
  • systemd 服务启动参数(flag)中指定了 hosts: [fd://]。

Docker 不允许 hosts 参数在两个地方同时定义,因此启动失败。

检查 systemd 配置文件:

Docker 的 systemd 配置文件可能通过 ExecStart 参数设置了默认的 fd://(文件描述符通信方式)。查看当前配置:

复制代码
systemctl cat docker

or

cat /lib/systemd/system/docker.service

查找 ExecStart 行,通常类似:

复制代码
ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

-H fd:// 是默认的 hosts 配置,与 daemon.json 中的配置冲突。

移除 systemd 中的 -H fd://

编辑 systemd 的 Docker 服务配置文件,避免重复定义 hosts

创建或编辑覆盖文件

复制代码
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo nano /etc/systemd/system/docker.service.d/override.conf

添加以下内容,移除 -H fd://:

复制代码
[Service]
ExecStart=
ExecStart=/usr/bin/dockerd --containerd=/run/containerd/containerd.sock

注意:ExecStart= 先清空原有配置,然后重新定义。

保存并重新加载 systemd 配置,重启 Docker 并检查状态:

复制代码
sudo systemctl daemon-reload
sudo systemctl restart docker
sudo systemctl status docker
相关推荐
阿里云云原生10 小时前
如何用 STAROps Elasticsearch 智能助手看穿“搜索变慢”
云原生
Alluxio10 小时前
Alluxio + OCI:为 AI 工作负载实现亚毫秒访问延迟与可扩展吞吐
运维·数据库·人工智能·分布式·缓存·ai·语言模型
雾时之林13 小时前
国内Docker 镜像加速器配置
云原生·eureka
Sayai14 小时前
Neo4j 图数据质量检查实战:环路检测、孤立节点与连通分量,两条 Cypher 引出的数据体检清单(含变长路径性能陷阱)
运维·neo4j·图数据库·数据质量·cypher
Elastic 中国社区官方博客15 小时前
更少的位数,相同的召回率:DiskBBQ 更保守的自动校准扫描
大数据·运维·开发语言·人工智能·elasticsearch·搜索引擎
方便面不加香菜15 小时前
Docker Volume -- 管理卷Volume详解
运维·docker·容器
沫璃染墨16 小时前
从零入门计算机网络系列(十八):TCP连接管理详解——从三次握手与四次挥手到TIME_WAIT机制
运维·服务器·网络·c++·网络协议·tcp/ip·tcp
Felven16 小时前
华为鲲鹏920s芯片serdestool工具使用说明
linux·运维·服务器·华为·鲲鹏920·serdestool
江湖有缘16 小时前
Docker实战 | 使用Docker部署lemonitor镜像站点列表程序
运维·docker·容器
莫晓燚16 小时前
mall 电商项目完整部署---实战
linux·运维·docker·容器