Docker 配置远程连接

docker是c/s架构的应用程序,默认仅监听socket格式的地址,只支持在本地管理。

若想要通过远程连接管理docker,需要在服务器端开启tcp监听方式。

Docker版本:19.03.1 系统版本:CentOS Linux release 7.6.1810 (Core) 主要修改的配置文件:/usr/lib/systemd/system/docker.service

默认配置

[root@test ~]# cat /usr/lib/systemd/system/docker.service
……
……
[Service]
Type=notify
# 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
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
……
……

修改后配置

[root@test ~]# cat /usr/lib/systemd/system/docker.service
……
……
[Service]
Type=notify
# 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 tcp://0.0.0.0:2375 -H fd:// --containerd=/run/containerd/containerd.sock
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
……
……

PS:这里主要添加了: -H tcp://0.0.0.0:2375 ,端口可自定义,远程连接时需要指定服务器地址+该端口。

重启服务

[root@test ~]# systemctl daemon-reload && systemctl  restart docker

查看监听端口和进程

[root@test ~]# ps -ef | grep docker
root     14793     1  0 15:34 ?        00:00:03 /usr/bin/dockerd -H tcp://0.0.0.0:2375 -H fd:// --containerd=/run/containerd/containerd.sock
root     24797  6852  0 15:43 pts/0    00:00:00 grep --color=auto docker
[root@test ~]# 
[root@test ~]# ss -ntl | grep 2375
LISTEN     0      128         :::2375                    :::*                  
[root@test ~]# 

测试

客户端远程管理docker。可通过 -H 或 --host 指定远程主机进行管理。

格式:

docker -H ServerIP:Port COMMAND

docker --host ServerIP:Port COMMAND

例如:

【Client端】
[root@ww ~]# docker -H 148.**.***.92:2375 image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
[root@ww ~]# 
[root@ww ~]# docker -H 148.**.***.92:2375 image pull nginx
Using default tag: latest
latest: Pulling from library/nginx
1ab2bdfe9778: Pull complete 
a17e64cfe253: Pull complete 
e1288088c7a8: Pull complete 
Digest: sha256:53ddb41e46de3d63376579acf46f9a41a8d7de33645db47a486de9769201fec9
Status: Downloaded newer image for nginx:latest
docker.io/library/nginx:latest
[root@ww ~]# docker -H 148.**.***.92:2375 image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              5a3221f0137b        2 weeks ago         126MB
[root@ww ~]# 

【Server端】
[root@test ~]# docker image ls
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               latest              5a3221f0137b        2 weeks ago         126MB
[root@test ~]# 

到这里docker-19.03.1 配置远程连接就已经成功了。

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

编辑于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券