在这以前咱们要知道docker是一个c/s架构的程序,也就是说咱们输入的docker命令其实是客户端用来发送指令给docker的守护进程的,全部的操做都是docker的守护进程来作。html
docker和服务端守护进程通讯有两种方式一个就是docker本身的client(docker cli)还有一个就是用户能够本身写程序,调用docker的remote-api来和docker的守护进程通讯。还有要知道的是docker客户端和服务端是使用socket链接方式的,有三种socket链接方式,一种是直接链接本地的socket文件unix:///var/run/docker/sock
,第二种是tcp://host:prot
,第三种是fd://socketfd
,默认docker都是直接链接本地的socket文件,并且还支持fd://socketfd
,若是咱们要支持远程链接就必需要加上tcp链接方式node
实验环境是 2台centos虚拟机,并且都安装上了docker,为了却别两台机器,我在服务端机器上pull了一个ubuntu镜像 docker pull ubuntu
以后修改服务端配置,让它支持使用tcp远程访问,这里说明一下docker1.12版本以前是修改vim /etc/default/docker
的DOCKER_OPTS参数的,可是1.12以后docker建议在/etc/docker/daemon.json
文件中修改docker启动参数,具体的全部参数以下面所示linux
{ "authorization-plugins": [], "data-root": "", "dns": [], "dns-opts": [], "dns-search": [], "exec-opts": [], "exec-root": "", "experimental": false, "storage-driver": "", "storage-opts": [], "labels": [], "live-restore": true, "log-driver": "", "log-opts": {}, "mtu": 0, "pidfile": "", "cluster-store": "", "cluster-store-opts": {}, "cluster-advertise": "", "max-concurrent-downloads": 3, "max-concurrent-uploads": 5, "default-shm-size": "64M", "shutdown-timeout": 15, "debug": true, "hosts": [], "log-level": "", "tls": true, "tlsverify": true, "tlscacert": "", "tlscert": "", "tlskey": "", "swarm-default-advertise-addr": "", "api-cors-header": "", "selinux-enabled": false, "userns-remap": "", "group": "", "cgroup-parent": "", "default-ulimits": {}, "init": false, "init-path": "/usr/libexec/docker-init", "ipv6": false, "iptables": false, "ip-forward": false, "ip-masq": false, "userland-proxy": false, "userland-proxy-path": "/usr/libexec/docker-proxy", "ip": "0.0.0.0", "bridge": "", "bip": "", "fixed-cidr": "", "fixed-cidr-v6": "", "default-gateway": "", "default-gateway-v6": "", "icc": false, "raw-logs": false, "allow-nondistributable-artifacts": [], "registry-mirrors": [], "seccomp-profile": "", "insecure-registries": [], "no-new-privileges": false, "default-runtime": "runc", "oom-score-adjust": -500, "node-generic-resources": ["NVIDIA-GPU=UUID1", "NVIDIA-GPU=UUID2"], "runtimes": { "cc-runtime": { "path": "/usr/bin/cc-runtime" }, "custom": { "path": "/usr/local/bin/my-runc-replacement", "runtimeArgs": [ "--debug" ] } } }
默认docker不建立这个文件,因此咱们要建立而且添加上咱们要的配置,不要的能够不加,好比个人就是docker
{ "registry-mirrors": ["https://kf0vxqi6.mirror.aliyuncs.com"], "labels": ["name=docker-server"], "hosts": [ "tcp://0.0.0.0:2376", "unix:///var/run/docker.sock" ] }
第一行是仓库地址,第二行是给docker-daemon作一个标签,第三行hosts就是链接方式,如今docker同时支持两种链接方式了 接着咱们直接在客户端centos机器上链接服务端的机器,输入下面命令 docker -H tcp://192.168.0.83:2376 info
-H后面就是指定链接的服务端地址 info表示查看服务端daemon的信息json
[root@MiWiFi-R1CM-srv ~]# docker -H tcp://192.168.0.83:2376 info Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 1 Server Version: 18.03.0-ce Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: cfd04396dc68220d1cecbe686a6cc3aa5ce3667c runc version: 4fc53a81fb7c994640722ac585fa9ca548971871 init version: 949e6fa Security Options: seccomp Profile: default Kernel Version: 3.10.0-693.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 1.797GiB Name: MiWiFi-R1CM-srv ID: 7S6I:U2LJ:C2KG:LVP7:D6WA:46KN:A2HB:XJ7G:6CI3:6ID3:3A5F:B7CZ Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: name=docker-server Experimental: false Insecure Registries: 127.0.0.0/8 Registry Mirrors: https://kf0vxqi6.mirror.aliyuncs.com/ Live Restore Enabled: false
若是你不想每次都输入-H参数,那么你能够在客户端机器加上下面的环境变量 export DOCKER_HOST="tcp://192.168.0.83:2376"
ubuntu
[root@MiWiFi-R1CM-srv ~]# export DOCKER_HOST="tcp://192.168.0.83:2376" [root@MiWiFi-R1CM-srv ~]# docker info Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 1 Server Version: 18.03.0-ce Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: cfd04396dc68220d1cecbe686a6cc3aa5ce3667c runc version: 4fc53a81fb7c994640722ac585fa9ca548971871 init version: 949e6fa Security Options: seccomp Profile: default Kernel Version: 3.10.0-693.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 1.797GiB Name: MiWiFi-R1CM-srv ID: 7S6I:U2LJ:C2KG:LVP7:D6WA:46KN:A2HB:XJ7G:6CI3:6ID3:3A5F:B7CZ Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: name=docker-server Experimental: false Insecure Registries: 127.0.0.0/8 Registry Mirrors: https://kf0vxqi6.mirror.aliyuncs.com/ Live Restore Enabled: false
若是你想链接本地那么改回环境变量便可vim
export DOCKER_HOST=""
centos
[root@MiWiFi-R1CM-srv ~]# export DOCKER_HOST="" [root@MiWiFi-R1CM-srv ~]# docker info Containers: 0 Running: 0 Paused: 0 Stopped: 0 Images: 0 Server Version: 18.03.0-ce Storage Driver: overlay2 Backing Filesystem: xfs Supports d_type: true Native Overlay Diff: true Logging Driver: json-file Cgroup Driver: cgroupfs Plugins: Volume: local Network: bridge host macvlan null overlay Log: awslogs fluentd gcplogs gelf journald json-file logentries splunk syslog Swarm: inactive Runtimes: runc Default Runtime: runc Init Binary: docker-init containerd version: cfd04396dc68220d1cecbe686a6cc3aa5ce3667c runc version: 4fc53a81fb7c994640722ac585fa9ca548971871 init version: 949e6fa Security Options: seccomp Profile: default Kernel Version: 3.10.0-693.el7.x86_64 Operating System: CentOS Linux 7 (Core) OSType: linux Architecture: x86_64 CPUs: 1 Total Memory: 1.797GiB Name: MiWiFi-R1CM-srv ID: 7S6I:U2LJ:C2KG:LVP7:D6WA:46KN:A2HB:XJ7G:6CI3:6ID3:3A5F:B7CZ Docker Root Dir: /var/lib/docker Debug Mode (client): false Debug Mode (server): false Registry: https://index.docker.io/v1/ Labels: name=docker-client-server Experimental: false Insecure Registries: 127.0.0.0/8 Registry Mirrors: https://kf0vxqi6.mirror.aliyuncs.com/ Live Restore Enabled: false
最后再说一句,若是你碰到了下面的问题 error during connect: Get http://192.168.0.83:2376/v1.37/info: dial tcp 192.168.0.83:2376: getsockopt: no route to host
那么就是你系统的防火墙开了,你只要修改防火墙规则或者关闭掉防火墙就行了,在centos下是 sudo systemctl stop firewalld
若是你还想查看更多关于链接的问题你能够查看下面这篇博客 远程链接docker daemon,Docker Remote APIapi
欢迎关注Bboysoul的博客www.bboysoul.com Have Funbash