开启Docker远程连接后,在开发和学习时都能带来便利,但请不要将此方式用在外部网络环境,以免带来安全问题,下图是官方警告:
环境信息
本次实战先在CentOS上的Docker设置,再在Ubuntu上也设置,首先要实战的CentOS环境信息如下:
操作系统:CentOS Linux release 7.6.1810Docker:1.13.1, build b2f74b2/1.13.1设置
1.编辑此文件:/lib/systemd/system/docker.service
原来的设置:
ExecStart=/usr/bin/dockerd-current –add-runtime docker-runc=/usr/libexec/docker/docker-runc-current –default-runtime=docker-runc –exec-opt native.cgroupdriver=systemd –userland-proxy-path=/usr/libexec/docker/docker-proxy-current –init-path=/usr/libexec/docker/docker-init-current –seccomp-profile=/etc/docker/seccomp.json $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY $REGISTRIES把ExecStart=/usr/bin/dockerd-current 改为ExecStart=/usr/bin/dockerd-current -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock ,如下:
ExecStart=/usr/bin/dockerd-current -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock –add-runtime docker-runc=/usr/libexec/docker/docker-runc-current –default-runtime=docker-runc –exec-opt native.cgroupdriver=systemd –userland-proxy-path=/usr/libexec/docker/docker-proxy-current –init-path=/usr/libexec/docker/docker-init-current –seccomp-profile=/etc/docker/seccomp.json $OPTIONS $DOCKER_STORAGE_OPTIONS $DOCKER_NETWORK_OPTIONS $ADD_REGISTRY $BLOCK_REGISTRY $INSECURE_REGISTRY $REGISTRIES修改完毕后保存退出;
2. 重新加载配并重启docker:
systemctl daemon-reload && systemctl restart docker现在设置完毕了,来验证设置是否生效;
3. 先在本机试试docke服务是否正常,如下,本机访问一切正常:
[root@maven ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE bolingcavalry/nacosconfigdemo 1.0-SNAPSHOT ed524b78f80b 5 days ago 125 MB bolingcavalry/nacosserver 0.0.1 9ab34ff860d8 3 weeks ago 156 MB docker.io/openjdk 8u212-jdk-stretch 03b20c1fa768 6 weeks ago 488 MB docker.io/openjdk 8u201-jdk-alpine3.9 3675b9f543c5 4 months ago 105 MB docker.io/fabric8/java-jboss-openjdk8-jdk 1.3 c9d7e2e7b917 20 months ago 436 MB4.本机IP地址是192.168.121.131,现在尝试从另一台装有docker的机器上测试远程访问本机,命令是docker -H tcp://192.168.121.131:2375 images,如下所示,一切正常:
[root@centos7 ~]# docker -H tcp://192.168.121.131:2375 images REPOSITORY TAG IMAGE ID CREATED SIZE bolingcavalry/nacosconfigdemo 1.0-SNAPSHOT ed524b78f80b 5 days ago 125 MB bolingcavalry/nacosserver 0.0.1 9ab34ff860d8 3 weeks ago 156 MB docker.io/openjdk 8u212-jdk-stretch 03b20c1fa768 6 weeks ago 488 MB docker.io/openjdk 8u201-jdk-alpine3.9 3675b9f543c5 4 months ago 105 MB docker.io/fabric8/java-jboss-openjdk8-jdk 1.3 c9d7e2e7b917 20 months ago 436 MB5.如果您觉得每次远程命令时都输入-H tcp://192.168.121.131:2375很麻烦,也可以将远程docker的IP信息存入环境变量DOCKER_HOST=“tcp://192.168.121.131:2375”,这样执行docker命令不带IP地址参数也会远程连接:
[root@centos7 ~]# export DOCKER_HOST=”tcp://192.168.121.131:2375″ [root@centos7 ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE bolingcavalry/nacosconfigdemo 1.0-SNAPSHOT ed524b78f80b 5 days ago 125 MB bolingcavalry/nacosserver 0.0.1 9ab34ff860d8 3 weeks ago 156 MB docker.io/openjdk 8u212-jdk-stretch 03b20c1fa768 6 weeks ago 488 MB docker.io/openjdk 8u201-jdk-alpine3.9 3675b9f543c5 4 months ago 105 MB docker.io/fabric8/java-jboss-openjdk8-jdk 1.3 c9d7e2e7b917 20 months ago 436 MBUbuntu上的设置
Ubuntu上的设置和CentOS略有不同,这里也一并说明:
环境: 操作系统:Ubuntu 18.04.2 LTSDocker:19.03.1(Community)步骤:
检查文件夹/etc/systemd/system/docker.service.d/是否存在,如果没有就创建;在文件夹/etc/systemd/system/docker.service.d/下新建文件override.conf,内容如下: [Service] ExecStart= ExecStart=/usr/bin/dockerd -H tcp://0.0.0.0:2375 -H unix://var/run/docker.sock ExecReload=/bin/kill -s HUP $MAINPID TimeoutSec=0 RestartSec=2 Restart=alway重新加载配置,再重启docker服务:
systemctl daemon-reload && systemctl restart docker.service现在Ubuntu上的Docker开启远程服务成功,可以像前面的步骤那样验证了。
至此,Docker远程连接的设置和验证就完成了,再次强调:此设置方式仅用于开发和学习,不要在公共网络这样设置,以免此Docker被其他未知的客户端远程连上,引发安全问题。
暂无评论内容