最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

从一台 Windows 10 上访问另一台 Windows 10上创建的Docker虚拟机

运维笔记admin0浏览0评论

访问 Docker for Windows Installer 的下载页面
https://docs.docker/docker-for-windows/install/#download-docker-for-windows
获取 Docker for Windows Installer 的下载链接
https://download.docker/win/stable/Docker%20for%20Windows%20Installer.exe
获取 Docker for Windows Installer 的摘要校验链接
https://download.docker/win/stable/Docker%20for%20Windows%20Installer.exe.sha256sum
下载 Docker for Windows Installer

Invoke-WebRequest https://download.docker/win/stable/Docker%20for%20Windows%20Installer.exe -OutFile ~\Downloads\"Docker for Windows Installer.exe"

下载 Docker for Windows Installer 的摘要校验

Invoke-WebRequest https://download.docker/win/stable/Docker%20for%20Windows%20Installer.exe.sha256sum -OutFile ~\Downloads\"Docker for Windows Installer.exe.sha256sum"

校验下载文件

Compare-Object (Get-FileHash ~\Downloads\"Docker for Windows Installer.exe" -Algorithm SHA256).Hash (Get-Content ~\Downloads\"Docker for Windows Installer.exe.sha256sum").Substring(0,64) -IncludeEqual

在第一台 Windows 10 上运行 Docker for Windows Installer

~\Downloads\"Docker for Windows Installer.exe"

默认安装 Docker for Windows 会自动启动,本例不使用自动启动,可以关闭 Docker for Windows 的自动启动选项
同样的在第二台 Windows 10 上运行 Docker for Windows Installer,本例不使用自动启动,可以关闭 Docker for Windows 的自动启动选项
在第二台 Windows 10 上的 Hyper-V 中创建虚拟交换机,设置名称为 myswitch
在第二台 Windows 10 上创建 Docker 虚拟机

docker-machine -D create -d hyperv --hyperv-virtual-switch "myswitch" --hyperv-memory  "576" s1-vm1

成功创建后在第二台 Windows 10 上可以看到文件夹结构为

~\.docker\machine
~\.docker\machine\cache
~\.docker\machine\cache\boot2docker.iso
~\.docker\machine\certs
~\.docker\machine\certs\ca.pem
~\.docker\machine\certs\ca-key.pem
~\.docker\machine\certs\cert.pem
~\.docker\machine\certs\key.pem
~\.docker\machine\machines
~\.docker\machine\machines\s1-vm1
~\.docker\machine\machines\s1-vm1\s1-vm1\(内部文件略)
~\.docker\machine\machines\s1-vm1\boot2docker.iso
~\.docker\machine\machines\s1-vm1\ca.pem
~\.docker\machine\machines\s1-vm1\cert.pem
~\.docker\machine\machines\s1-vm1\config.json
~\.docker\machine\machines\s1-vm1\disk.vmd
~\.docker\machine\machines\s1-vm1\disk_(文件名略).avhd
~\.docker\machine\machines\s1-vm1\id_rsa
~\.docker\machine\machines\s1-vm1\id_rsa.pub
~\.docker\machine\machines\s1-vm1\key.pem
~\.docker\machine\machines\s1-vm1\server.pem
~\.docker\machine\machines\s1-vm1\server-key.pem
~\.docker\daemon.json

把文件复制到第一台 Windows 10 上,文件夹结构为

~\.docker\machine
~\.docker\machine\cache
~\.docker\machine\s1-certs
~\.docker\machine\s1-certs\ca.pem
~\.docker\machine\s1-certs\ca-key.pem
~\.docker\machine\s1-certs\cert.pem
~\.docker\machine\s1-certs\key.pem
~\.docker\machine\machines
~\.docker\machine\machines\s1-vm1
~\.docker\machine\machines\s1-vm1\ca.pem
~\.docker\machine\machines\s1-vm1\cert.pem
~\.docker\machine\machines\s1-vm1\config.json
~\.docker\machine\machines\s1-vm1\id_rsa
~\.docker\machine\machines\s1-vm1\id_rsa.pub
~\.docker\machine\machines\s1-vm1\key.pem
~\.docker\machine\machines\s1-vm1\server.pem
~\.docker\machine\machines\s1-vm1\server-key.pem
~\.docker\daemon.json

其中第一台 Windows 10 上的 ~\.docker\machine\s1-certs 对应于第二台 Windows 10 上的 ~\.docker\machine\certs ,文件夹改名的原因在于第一台 Windows 10 以后创建虚拟机的时候会使用这个文件夹。
其中第一台 Windows 10 上的 ~\.docker\machine\machines\s1-vm1 复制自第二台 Windows 10 上的对应文件夹的对应文件,其他虚拟机文件就不用复制了
然后编辑 config.json 文件,配置文件中出现的所有的文件路径都修改为对应的完整的文件路径,有些可能不需要了,不过也一起顺手改了吧。修改其中的 DriverNamegeneric

"DriverName": "generic",

此时在 PowerShell 中运行命令docker-machine ls
即可看到识别出这台设备

NAME     ACTIVE   DRIVER    STATE     URL                        SWARM   DOCKER        ERRORS
s1-vm1   *        generic   Running   tcp://192.168.1.100:2376           v18.01.0-ce

如果希望使用 docker 命令,还需要使用 docker-machine 命令生成环境变量并运行,以便于后续的的 docker 命令直接使用,注意此处为了解决中文问题做了转码,

foreach ($line in (docker-machine env s1-vm1))
{
[System.Text.Encoding]::GetEncoding("utf-8").GetString([System.Text.Encoding]::GetEncoding("gb2312").GetBytes($line)) |  Invoke-Expression
}

在第一台 Windows 10 上运行祖传开光祈福命令

docker run hello-world

可以看到运行结果

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
ca4f61b1923c: Pull complete
Digest: sha256:66ef312bbac49c39a89aa9bcc3cb4f3c9e7de3788c944158df3ee0176d32b751
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://cloud.docker/

For more examples and ideas, visit:
 https://docs.docker/engine/userguide/

在第二台 Windows 10 上运行如上生成环境变量的命令,然后查看

docker ps -a

可以看到运行结果

CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
4bf2e5bd62dc        hello-world         "/hello"            15 minutes ago      Exited (0) 15 minutes ago                       kind_knuth

这就是在第一台 Windows 10 上调用命令的结果

发布评论

评论列表(0)

  1. 暂无评论