就在上周,Docker官方的master分支上新增了LCOW(Linux Containers on Windows)[1]功能。启用这项功能,即可在单一的Docker引擎下,同时运行Linux和Windows容器。下面赶紧跟小编一起,看看Windows 10将会发生哪些变化?
现在,你需要指定--platform来拉取Linux镜像。如果拉取的是一个既有Linux又有Windows的多重架构的镜像,同样需要指定该选项。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容器通过主机的一个共享文件夹,实现数据共享。cd \
mkdir host
在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 10主机上启动一个Windows容器,并且将主机上的共享文件夹挂载到该容器的C:\test文件夹。
docker run -i -v C:\host:C:\test microsoft/nanoserver:1709 cmd
我们在C:\test文件夹下,新建一个名为hello-from-windows.txt的文件。ver > test\hello-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
在开发过程中,如果需要共享配置文件或代码的话,这实在是太方便了有木有~
值得一提的是,使用Docker Compose还可混合编排容器。下面是一个混合编排Linux和Window web服务器的例子。 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,建议使用Windows 10 1709虚拟机。我在微软Azure上用Windows 10 1709虚拟机测试过LCOW。你可以选择具有嵌套式hypervisor的V3 机器以便运行Hyper-V容器。
Enable-WindowsOptionalFeature -Online -FeatureName containers -All -NoRestart
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V -All -NoRestart
然后,为你的LCOW安装一个LinuxKit镜像。下面是我下载的最新版LinuxKit,你也可以自行从linuxkit/lcow[2]下载。Invoke-WebRequest -OutFile "$env:TEMP\linuxkit-lcow.zip" "https://23-111085629-gh.circle-artifacts.com/0/release.zip"
Expand-Archive -Path "$env:TEMP\linuxkit-lcow.zip" -DestinationPath "$env:ProgramFiles\Linux Containers" -Force
Invoke-WebRequest -OutFile "$env:TEMP\docker-master.zip" "https://master.dockerproject.com/windows/x86_64/docker.zip"
Expand-Archive -Path "$env:TEMP\docker-master.zip" -DestinationPath $env:ProgramFiles -Force
接下来,安装Docker service并打开实验功能。 . $env:ProgramFiles\docker\dockerd.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安装了Hyper-V或VMware,那就更方便了。只需几个命令,就可轻松搭建本地测试环境。你只需要从我的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新功能加入到Windows,Windows 10无疑将成为2018年最具吸引力的开发者平台。想象一下,你用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
原文链接:https://stefanscherer.github.io/sneak-peek-at-lcow/