https://blog.csdn.net/m2l0zgssvc7r69efdtj/article/details/79251059
下面赶紧跟小编一起,看看Windows 10将会发生哪些变化?
-
可以用Docker命令docker ps,列出所有正在运行的Linux或Windows容器。
-
在容器和主机之间通过存储卷共享数据。
-
容器之间可以通过容器网络互相通信。
-
通过将端口映射到主机,实现本地访问。但目前,它还只是Windows 10 1803版预览体验计划(Windows Insider)的一项功能。
运行Linux容器
docker pull --platform linux alpine
镜像拉取完毕即可运行,无需指定--platform选项。
docker run alpine uname -a
另外,Windows上运行Linux容器需要一台小型的Hyper-V虚拟机。同时,LinuxKit项目提供了LCOW的镜像,请参照:https://github.com/linuxkit/lcow。
共享存储
方法是把Linux和Windows容器,绑定到同一个存储卷。
下面的例子中,Linux和Windows容器通过主机的一个共享文件夹,实现数据共享。
首先,在Windows 10 上新建一个文件夹。
cd
mkdir host
启动Linux容器
在Windows 10主机上启动一个Linux容器,并且将主机上的共享文件夹挂载到该容器的/test文件夹。
docker run -it -v C:host:test alpine sh
我们在/test文件夹下,新建一个名为hello-from-linux.txt的文件。
uname -a > test/hello-from-linux.txt
启动Windows容器
在Windows 10主机上启动一个Windows容器,并且将主机上的共享文件夹挂载到该容器的C: est文件夹。
docker run -i -v C:host:C:test microsoft/nanoserver:1709 cmd
我们在C: est文件夹下,新建一个名为hello-from-windows.txt的文件。
ver > testhello-from-windows.txt
测试结果
上述两个容器中新建的文件,都保存在Windows 10 主机的共享文件夹。
PS C:> dir host
Directory: C:host
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 1/21/2018 4:32 AM 85 hello-from-linux.txt
-a---- 1/21/2018 4:33 AM 46 hello-from-windows.txt
在开发过程中,如果需要共享配置文件或代码的话,这实在是太方便了有木有~
混合编排
version: "3.2"
services:
web1:
image: nginx
volumes:
- type: bind
source: C:host
target: /test
ports:
- 80:80
web2:
image: stefanscherer/hello-dresden:0.0.3-windows-1709
volumes:
- type: bind
source: C:host
target: C:test
ports:
- 81:3000
networks:
default:
external:
name: nat
你也可以思考一下,如何编排一个Linux数据库和Windows前端,反过来也一样。
如何搭建你的LCOW测试环境
Azure
我在微软Azure上用Windows 10 1709虚拟机测试过LCOW。你可以选择具有嵌套式hypervisor的V3 机器以便运行Hyper-V容器。
容器和Hyper-V
首先,启用容器和Hyper-V功能:
Enable-WindowsOptionalFeature -Online -FeatureName containers -All -NoRestart
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart
LinuxKit
然后,为你的LCOW安装一个LinuxKit镜像。下面是我下载的最新版LinuxKit,你也可以自行从linuxkit/lcow[2]下载。
Invoke-WebRequest -OutFile "$env:TEMPlinuxkit-lcow.zip" "https://23-111085629-gh.circle-artifacts.com/0/release.zip"
Expand-Archive -Path "$env:TEMPlinuxkit-lcow.zip" -DestinationPath "$env:ProgramFilesLinux Containers" -Force
创建Docker
现在,你可以下载并安装Docker引擎了。
Invoke-WebRequest -OutFile "$env:TEMPdocker-master.zip" "https://master.dockerproject.com/windows/x86_64/docker.zip"
Expand-Archive -Path "$env:TEMPdocker-master.zip" -DestinationPath $env:ProgramFiles -Force
接下来,安装Docker service并打开实验功能。
. $env:ProgramFilesdockerdockerd.exe --register-service --experimental
紧接着,配置PATH以便使用Docker CLI。
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";$($env:ProgramFiles)docker", [EnvironmentVariableTarget]::Machine)
恭喜你!现在可以重启机器了。重启过程中会自动更新容器和Hyper-V的配置。如果一切顺利,重启完后Docker engine应该已经起来并处于运行状态了,这时你可以在PowerShell终端使用Docker CLI。
本地的Vagrant环境
你只需要从我的GitHub下载包含LCOW环境的docker-windows-box,执行以下命令就可以了。
git clone https://github.com/StefanScherer/docker-windows-box
cd docker-windows-box
cd lcow
vagrant up
如果你的Windows 10虚拟机上没有Vagrant base box,系统会自动下载安装。如果已经下载了,则只进行安装。
总结
想象一下,你用docker-compose.yml编排Linux和Windows容器,通过Visual Studio Code实时调试你的应用程序,等等。
如果您喜欢这篇文章,请分享给您的朋友。如果想了解Windows容器的最新情况,请关注原文作者的推特@stefscherer[3]。
相关链接:
-
https://github.com/moby/moby/pull/34859
-
https://github.com/linuxkit/lcow/releases
-
https://twitter.com/stefscherer