docker的参考文档:https://docs.docker.com/install/linux/linux-postinstall/#configure-where-the-docker-daemon-listens-for-connections
有2种方式配置docker daemon端口
1) 在/etc/default/docker文件中配置:
DOCKER_OPTS="-H tcp://127.0.0.1:5000 -H unix:///var/run/docker.sock"
2) 在/etc/docker/daemon.json进行配置:
{
"debug": true,
"hosts": ["tcp://127.0.0.1:5000", "unix:///var/run/docker.sock"]
}
如果没有配置 docker 默认套接字,Docker 将无限期等待。即
Waiting for /var/run/docker.sock
Waiting for /var/run/docker.sock
Waiting for /var/run/docker.sock
Waiting for /var/run/docker.sock
Waiting for /var/run/docker.sock
注意:但不要在两个配置文件中配置,可能会出现以下错误:
Waiting for /var/run/docker.sock
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: [tcp://127.0.0.1:5000 unix:///var/run/docker.sock], from file: tcp://127.0.0.1:5000)
同时添加用户端口[tcp://127.0.0.1:5000]和默认docker socket[unix:///var/run/docker.sock]的原因 是用户端口启用对 docker API 的访问,而默认套接字启用 CLI。如果 /etc/default/docker 文件中没有提到默认端口[unix:///var/run/docker.sock],可能会出现以下错误:
# docker ps
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
这个错误不是因为docker没有运行,而是因为默认docker socket没有启用。
启用配置后,重启 docker 服务并验证 docker 端口是否启用:
# netstat -tunlp | grep -i 5000
tcp 0 0 127.0.0.1:5000 0.0.0.0:* LISTEN 31661/dockerd
适用于 Docker 版本 17.04,可能因 docker 版本不同而异。