Docker Machine实战
Docker Machine 是一种可以让您在虚拟主机上安装 Docker 的工具,并可以使用 docker-machine 命令来管理主机。
Docker Machine 也可以集中管理所有的 docker 主机,比如快速的给 100 台服务器安装上 docker。
下面我们就使用docker-machine为另外一台机器安装docker,并创建容器以及拉去镜像
机器列表
ip | 系统 | 备注 |
---|---|---|
192.168.0.104 | mac | 安装docker-machine,且必须安装过docker |
192.168.0.109 | centos | 本地搭建自己的虚拟机 |
第一步:mac电脑安装docker-machine(已经安装过docker)
$ base=https://github.com/docker/machine/releases/download/v0.16.0 &&
curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/usr/local/bin/docker-machine &&
chmod +x /usr/local/bin/docker-machine
第二步:两台机器都关闭防火墙,以及centos修改ssh和sudoers文件
mac电脑关闭防火墙
centos关闭防火墙以及修改ssh和sudoers文件
systemctl stop firewalld
[root@localhost ~]# vim /etc/ssh/sshd_config
# 将以下取消注释,表示允许root权限登录操作
PermitRootLogin yes
[root@localhost ~]# systemctl restart sshd
[root@localhost ~]# vim /etc/sudoers
# 将以下内容去掉!符号
Defaults !visiblepw
第三步:免密登录
mac电脑对192.168.0.109进行免密登录
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/wangxuan/.ssh/id_rsa):
/Users/wangxuan/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/wangxuan/.ssh/id_rsa.
Your public key has been saved in /Users/wangxuan/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ZAskg1cVUWyRvjkQvFt4FS/p28wlzojUnPSm7NhAsHw wangxuan@wangxuandeMacBook-Air.local
The key's randomart image is:
+---[RSA 2048]----+
| .o.oo+=oo.. |
| . .+ o + .o |
| . . +* .+ . |
| =+++= + |
| S=Eo= + .|
| .++o @ o |
| o.= * |
| = |
| . o |
+----[SHA256]-----+
$ ssh-copy-id -i root@192.168.0.109
$ ssh root@192.168.0.109 (验证是否免密登录)
第四步:mac电脑为主机192.168.0.109安装docker
$ docker-machine create --driver generic --generic-ip-address 192.168.0.109 host1
Running pre-create checks...
Creating machine...
(host1) No SSH key specified. Assuming an existing key at the default location.
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 centos...
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 host1
第五步:验证是否安装成功
$ docker-machine ls
NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS
host1 - generic Running tcp://192.168.0.109:2376 v23.0.3
第六步:本地mac电脑操作给主机192.168.0.109创建容器和拉去镜像
$ docker-machine env host1 (查看host1需要的环境变量)
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.0.109:2376"
export DOCKER_CERT_PATH="/Users/wangxuan/.docker/machine/machines/host1"
export DOCKER_MACHINE_NAME="host1"
# Run this command to configure your shell:
# eval $(docker-machine env host1)
$ eval $(docker-machine env vm)
Docker machine "vm" does not exist. Use "docker-machine ls" to list machines. Use "docker-machine create" to add a new one.
$ eval $(docker-machine env host1)(可进入host1环境中)
$ docker run -d --name=nginx nginx (本地拉去镜像)
Unable to find image 'nginx:latest' locally
latest: Pulling from library/nginx
26c5c85e47da: Pull complete
4f3256bdf66b: Pull complete
2019c71d5655: Pull complete
8c767bdbc9ae: Pull complete
78e14bb05fd3: Pull complete
75576236abf5: Pull complete
Digest: sha256:63b44e8ddb83d5dd8020327c1f40436e37a6fffd3ef2498a6204df23be6e7e94
Status: Downloaded newer image for nginx:latest
bf3306bfb9eb4fa3e372bf26802240e07909e6cdef3c2a5c4ad9751a4a0fb0a8
$ docker ps -a (查看镜像)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf3306bfb9eb nginx "/docker-entrypoint.…" 6 seconds ago Up 5 seconds 80/tcp nginx
第七步:进入192.168.0.109主机查看是否有镜像
$ docker-machine ssh host1 (登录主机host1)
Last login: Sat Apr 15 16:28:25 2023 from 192.168.0.104
[root@host1 ~]# docker ps -a (发现容器id和mac的容器一样)
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
bf3306bfb9eb nginx "/docker-entrypoint.…" 36 seconds ago Up 35 seconds 80/tcp nginx
[root@host1 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx latest 6efc10a0510f 3 days ago 142MB
总结
Docker Engine Linux 系统上原生地运行。如果你有一个 Linux 作为你的主系统,并且想要运行 docker 命令,所有你需要做的就是下载并安装 Docker Engine 。然而,如果你想要在网络上、云中甚至本地配置多个 Docker 宿主机有一个有效的方式,你需要 Docker Machine。
无论你的主系统是 Mac、Windows 还是 Linux,你都可以在其上安装 Docker Machine,并使用 docker-machine 命令来配置和管理大量的 Docker 宿主机。它会自动创建宿主机、在其上安装 Docker Engine 、然后配置 docker 客户端。每个被管理的宿主机(“machine”)是 Docker 宿主机和配置好的客户端的结合