說明
ubuntu 系統類型
test docker、docker-machine管理操作用戶
192.168.1.73 docker-machine服務器端
192.168.1.80 docker 客戶端
1sudo配置:
執行范圍:docker-machine服務器端、docker 客戶端
test@bogon:~$ sudo visudo
#追加1行,確保遠程ssh執行命令不報錯
#sudo: no tty present and no askpass program specified
Defaults visiblepw
#文件末尾追加 sudo無需密碼
test ALL=(ALL) NOPASSWD: ALL
二、docker-machine服務器端配置
2.1 分發公鑰
生成私鑰、公鑰方法自行google
scp .ssh/authorized_keys 192.168.1.80:./ssh/
2.2 下載安裝
安裝docker
curl -fsSL https://get.docker.com/ | sudo sh
安裝docker-machine
curl -L https://github.com/docker/machine/releases/download/v0.9.0-rc2/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine sudo mv /tmp/docker-machine /usr/local/bin/ sudo chmod a+x /usr/local/bin/docker-machine
2.3 創建docker主機
test@bogon:~$ docker-machine create --driver generic --generic-ip-address=192.168.1.80 --generic-ssh-key ~/.ssh/id_rsa --generic-ssh-user=test 80 Running pre-create checks... Creating machine... (80) Importing SSH key... Waiting for machine to be running, this may take a few minutes... Detecting operating system of created instance... Waiting for SSH to be available... Detecting the provisioner... Provisioning with ubuntu(systemd)... Installing Docker... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon... Checking connection to Docker... Docker is up and running! To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env 80
docker-machine詳細命令參見:https://docs.docker.com/machine/
命令分析:
create #創建docker主機
--driver generic #驅動類型 generic 支持linux通用服務器,還支持很多種雲主機
--generic-ip-address=192.168.1.80 #指定主機
--generic-ssh-key ~/.ssh/id_rsa #指定私鑰
--generic-ssh-user=test #指定用戶
80 #主機名稱
查看已創建docker主機
test@bogon:~$ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS 73 * generic Running tcp://192.168.1.73:2376 v17.06.0-ce 80 - generic Running tcp://192.168.1.80:2376 v17.06.0-ce
遠程執行docker命令,創建docker虛擬機
test@bogon:~$ docker-machine ssh 80 docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 691bd1e50cd2 hello-world "/hello" 2 days ago Exited (0) 2 days ago tender_banach test@bogon:~$ docker-machine ssh 80 docker run hello-world test@bogon:~$ docker-machine ssh 80 docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 68399de0b83b hello-world "/hello" 5 seconds ago Exited (0) 4 seconds ago thirsty_bohr 691bd1e50cd2 hello-world "/hello" 2 days ago Exited (0) 2 days ago tender_banach
也可通過切換環境變量,來實現:
test@bogon:~$ eval $(docker-machine env 80) test@bogon:~$ docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS 73 - generic Running tcp://192.168.1.73:2376 v17.06.0-ce 80 * generic Running tcp://192.168.1.80:2376 v17.06.0-ce test@bogon:~$ docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
68399de0b83b hello-world "/hello" 5 seconds ago Exited (0) 4 seconds ago thirsty_bohr 691bd1e50cd2 hello-world "/hello" 2 days ago Exited (0) 2 days ago tender_banach