首页 公告 项目 RSS

使用本地的docker客户端连接远程docker的守护进程

April 12, 2018 本文有 1604 个字 需要花费 4 分钟阅读

概述

在这之前我们要知道docker是一个c/s架构的程序,也就是说我们输入的docker命令实际上是客户端用来发送指令给docker的守护进程的,所有的操作都是docker的守护进程来做。

一些基础知识

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连接方式

使用docker本地客户端连接远程docker服务端

实验环境是 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启动参数,具体的所有参数如下面所示

{
	"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不创建这个文件,所以我们要创建并且添加上我们要的配置,不要的可以不加,比如我的就是

{
  "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的信息

[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"

[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

如果你想连接本地那么改回环境变量即可

export DOCKER_HOST=""

[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 API

欢迎关注我的博客www.bboy.app
Have Fun