Docker的Windows容器初体验

Docker的Windows容器初体验

易立 2016-10-26 22:49:54 浏览61825 评论9

摘要: 最近微软发布了Windows Server 2016,其中最让广大开发者和运维同学期待的就是Docker对Windows容器的支持。如果你手边没有Windows Server 2016的环境,我们也可以在Windows 10 操作系统上,使用Docker for Windows来开始实验。

docker_windows_logo

系列文章

最近微软发布了Windows Server 2016,其中最让广大开发者和运维同学期待的就是Docker对Windows容器的支持。

Windows支持两种不同类型的容器,即 Windows Server 容器和 Hyper V 容器。 这两种类型的容器的使用方式相同,也支持相同的容器映像。 但是它们的实现机制不同,提供了不同的安全隔离级别

  • Windows Server 容器 - 非常类似与Linux中的容器,使用通过命名空间、资源控制实现进程隔离。每个Windows Server容器都与宿主机共享同一个内核。
  • Hyper V 容器 - 每个容器都运行在一个高度优化的Hyper V虚拟机中,所以容器拥有独立的内核。这样容器的隔离性会更好,但是启动速度会慢一些,其资源占用也会增加。

如果你手边没有Windows Server 2016的环境,我们也可以在Windows 10 操作系统上,使用Docker for Windows来开始实验。

前提条件

与基于Virtualbox的Docker Toolbox/Machine版不同,Docker for Windows 依赖于微软的虚拟化技术Hyper V。64位的Windows 10在专业版、企业版和教育版中,提供了Hyper V支持,但如果您使用的是家庭版,请升级后再做实验。

您可以通过控制面板的“程序“ > “启用或关闭Windows功能”来检查Hyper-V状态,中选中并开启Hyper V。Docker for Windows也会在安装过程中自动开启相应设置。

hyper_v

另外你需要为Windows 10安装2016年度更新,来确保在Window内核已提供原生的Windows容器支持。

注:Windows 10目前只支持Hyper V类型的Windows容器。

安装Docker for Windows Beta

与之前Docker Engine不同,最新的Docker for Windows提供了对Linux容器和Windows容器的支持。但值得注意的是:这两种容器模式不能被Docker Engine同时启用,但可以手工切换。

目前Docker for Windows对Windows容器的支持只在Beta版本中存在,你需要在下载链接中点击Get Docker for Windows (Beta) 来进行安装

设置Docker for Windows

在安装之后,我们启动PowerShell就可以通过Docker命令来管理容器。我们首先执行docker version命令来检查Docker的版本和系统信息,这时我们可以发现Docker Engine运行在Linux容器模式。

PS C:\Users\denve> docker version
Client:
 Version:      1.12.2
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   bb80604
 Built:        Tue Oct 11 05:27:08 2016
 OS/Arch:      windows/amd64
 Experimental: true

Server:
 Version:      1.12.2
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   bb80604
 Built:        Tue Oct 11 05:27:08 2016
 OS/Arch:      linux/amd64
 Experimental: true

为了切换到Windows容器模式,我们先点击桌面托盘,再从Docker图标的右键菜单中选中 “Switch to Windows containers ...” 片刻之后,Windows 容器环境会就绪。

switch

再次执行docker version 命令,这时Server的“OS/Arch”已经从“linux/amd64”切换为“windows/amd64”,Docker Engine已经运行在Windows容器模式

PS C:\Users\denve> docker version
Client:
 Version:      1.12.2
 API version:  1.24
 Go version:   go1.6.3
 Git commit:   bb80604
 Built:        Tue Oct 11 05:27:08 2016
 OS/Arch:      windows/amd64
 Experimental: true
Server:
 Version:      1.12.2-cs2-ws-beta
 API version:  1.25
 Go version:   go1.7.1
 Git commit:   050b611
 Built:        Tue Oct 11 02:35:40 2016
 OS/Arch:      windows/amd64

注:在Linux容器模式下,我们可以在Docker图标的右键菜单中选中“Settings...” 对Docker Engine进行更多设置,比如可以在“Docker Daemon”中设置Docker Hub的镜像地址,这样可以利用阿里云容器镜像服务提供的加速器来加快从镜像下载速度。但是这些设置对于Windows容器模式目前无效

settings

注: 如果开启了Windows安全防护软件,在拉取镜像时有可能会报如下错误,请关闭掉安全软件再试。

failed to register layer: re-exec error: exit status 1: output: Failed to OpenForBackup failed in Win32:

开启Windows容器之旅

下面我们来做最简单的Docker操作,启动一个示例容器

PS C:\Users\denve> docker run microsoft/sample-dotnet

        Welcome to .NET Core!
    __________________
                      \
                       \
                          ....
                          ....'
                           ....
                        ..........
                    .............'..'..
                 ................'..'.....
               .......'..........'..'..'....
              ........'..........'..'..'.....
             .'....'..'..........'..'.......'.
             .'..................'...   ......
             .  ......'.........         .....
             .                           ......
            ..    .            ..        ......
           ....       .                 .......
           ......  .......          ............
            ................  ......................
            ........................'................
           ......................'..'......    .......
        .........................'..'.....       .......
     ........    ..'.............'..'....      ..........
   ..'..'...      ...............'.......      ..........
  ...'......     ...... ..........  ......         .......
 ...........   .......              ........        ......
.......        '...'.'.              '.'.'.'         ....
.......       .....'..               ..'.....
   ..       ..........               ..'........
          ............               ..............
         .............               '..............
        ...........'..              .'.'............
       ...............              .'.'.............
      .............'..               ..'..'...........
      ...............                 .'..............
       .........                        ..............
        .....

构建一个测试Docker镜像,其Dockerfile文件如下

FROM microsoft/nanoserver
CMD echo Hello World!

构建镜像并执行的结果如下

PS C:\Users\denve\test> docker build -t test .
Sending build context to Docker daemon 2.048 kB
Step 1/2 : FROM microsoft/nanoserver
 ---> e14bc0ecea12
Step 2/2 : CMD echo Hello World!
 ---> Running in 0b0831046879
 ---> 3e506bc77617
Removing intermediate container 0b0831046879
Successfully built 3e506bc77617
PS C:\Users\denve\test> docker run test
Hello World!

感觉是不是还不错,Docker为Windows容器和Linux容器提供了一致的用户体验。

测试Docker Compose

微软提供了一些官方的Windows容器示例,可以从下列项目获得 https://github.com/Microsoft/Virtualization-Documentation/tree/live/windows-container-samples/

我们可以利用docker-compose创建一个由asp.net博客应用和MS SQLServer构成的多容器应用

首先,下载示例代码

git clone https://github.com/Microsoft/Virtualization-Documentation
cd .\Virtualization-Documentation\windows-container-samples/ASP-NET-Blog-Application

在“web/Dockerfile” 中添加如下一行

RUN powershell -Command Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\Dnscache\Parameters' -Name ServerPriorityTimeLimit -Value 0 -Type DWord

这时因为目前Windows容器的DNS解析还存在问题,有时不能从web容器中正确解析到“db“”服务的容器,导致应用无法正常启动。社区中已有讨论 https://github.com/docker/docker/issues/27499 ,这里我们采用了文中提到的临时解决方案。

注:如果你使用Docker 1.13以上版本,需要将docker-compose.yml中的version: '2'修改为version: '2.1',否则会提示如下错误

ERROR: client version 1.22 is too old. Minimum supported API version is 1.24, please upgrade your client to a newer version

然后我们构建Docker应用镜像,并利用Docker Compose模板启动整个应用

docker-compose build
docker-compose up

执行完毕,我们可以通过docker compose ps命令查看容器状态

           Name                          Command               State         Ports
-----------------------------------------------------------------------------------------
aspnetblogapplication_db_1    cmd /S /C powershell ./sta ...   Up      1433/tcp
aspnetblogapplication_web_1   c:\windows\system32\cmd.exe      Up      0.0.0.0:80->80/tcp

使用下列命令,我们可以获得web服务容器的IP地址

docker inspect aspnetblogapplication_web_1

这时我们就可以通过 http://<container-ip>/BlogEngine 来访问Blog应用了!

blog

总结

Windows容器的出现大大简化了Windows应用交付和运维的复杂性,对于微软的技术生态有着及其重要的意义。Docker在简化用户体验上做足了文章,现有工具链可以对Windows和Linux容器环境提供接近一致的能力。

同时Windows和Linux容器底层的实现机制还有很多不同,比如容器网络,基础镜像选择都有特别之处。未来我们会逐渐介绍。阿里云容器服务也会在未来的版本中提供对Windows容器的支持

想了解更多容器服务内容,请访问 https://www.aliyun.com/product/containerservice

本文为云栖社区原创内容,未经允许不得转载,如需转载请发送邮件至yqeditor@list.alibaba-inc.com;如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:yqgroup@service.aliyun.com 进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。

【云栖快讯】阿里巴巴小程序繁星计划,20亿补贴第一弹云应用立即开通购买,限量从速!  详情请点击

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker on Windows by Elton Stoneman English | 14 July 2017 | ISBN: 1785281658 | 358 Pages | AZW3/EPUB/MOBI/PDF (conv) | 23.77 MB Key Features Start building applications as Docker images and running containers in a Windows production environment Design and implement distributed applications that run across multiple networked containers Build a full Continuous Deployment pipeline for a .NET Core application running on Windows Book Description Docker has become hugely popular because it offers benefits at all stages of software design and delivery. Until recently, it was impossible to use a Windows Server platform to host the Docker Engine without adding an additional layer of virtualization. Starting with Windows Server 2016, you are able to use Docker on Windows and take advantage to easily build, deploy, and run Windows Containers. From building and running simple images to designing and implementing distributed applications, this book will teach everything you need to know while using Docker on Windows. You will start by installing Docker on different versions of Windows and Windows Server, and run a simple container to verify that the setup is working correctly. You will see how to build Docker images with a Dockerfile for a simple website, and then run it on Windows. Moving on, we'll cover the practical usage of Docker and the tools in the Docker ecosystem, such as Docker Compose, Docker Trusted Registry, and Docker Swarm Mode. Then, you'll be able to apply that knowledge by building a full continuous deployment pipeline for a .NET Core application running on Windows. We'll also dive deep into some of the core features of Docker, such as networking and security. At the end of the book, you'll be guided through a business case that will help you practically implement Docker in a real-world scenario. What you will learn Understand how Docker works, build images, and run containers Run Docker on different versions of Windows, and on the cloud with Azure Des

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值