使用本地的docker客戶端鏈接遠程docker的守護進程

概述

在這以前咱們要知道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

使用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啓動參數,具體的全部參數以下面所示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

相關文章
相關標籤/搜索