您的位置:首页 > 运维架构 > Docker

windows10/2016上使用docker

2016-01-29 10:31 811 查看
该文章是在一个物理主机使用windows server 2016或windows 10上使用docker非hyper-v虚拟机。
首先搭建一个容器主机。

按照微软的官方文档搭建该容器主机,步骤如下:

安装容器功能
PS C:\> start-process powershell -verb runas
PS C:\> install-windowsfeature containers
PS C:\> shutdown -r -t 0
PS C:\> get-containerhost

启用hyper-v角色
PS C:\> install-windowsfeature hyper-v

创建虚拟交换机
PS C:\> new-vmswitch -name "DHCP" -switchtype external(这是使用的桥接方式,还可以使用nat方式)
PS C:\> new-vmswitch -name "virtual switch" -switchtype nat -natsubnetaddress 172.16.0.1/24

如果虚拟交换机配置的是nat类型,则需要创建nat对象。
PS C:\> new-netnat -name containernat -internalipinterfaceaddressprefix "172.16.0.1/24"

安装操作系统镜像
PS C:\> install-packageprovider containerprovider -force
ps C:\> find-containerimage
ps C:\> install-containerimage -name nanoserver
ps C:\> install-containerimage -name windowservercore
安装操作系统镜像的原理是使用微软提供的BITS服务,从其网站上下载一个.wim的文件然后进行转换成该容器的镜像的方式,存放的目录默认在C:\programdata\microsoft\windows\image。下载的目录在C:\Windows\winsxs\
如果找到在哪个下载地址的话请告诉我。
ps C:\> get-containerimage

Name Publisher Version IsOSImage
---- --------- ------- ---------
NanoServer CN=Microsoft 10.0.10586.0 True
WindowsServerCore CN=Microsoft 10.0.10586.0 True

然后按照该文档在windows上安装docker及docker服务。 https://msdn.microsoft.com/zh-cn/virtualization/windowscontainers/deployment/docker_windows
容器主机和docker准备完以后就可以在该主机上运行容器了,一种方法是使用powershell运行一个容器,一种方法是使用docker运行容器。两种方法都差不多。具体的见如下命令。
PS C:\> get-containerimage
PS C:\> new-container -name tst -containerimagename windowsservercore
ps c:\> add-containernetworkadapter -containername tst
ps c:\> get-vmswitch
ps c:\> connect-containernetworkadapter -containername tst -switchname DHCP
ps c:\> $container=get-container -name tst(如果这里不定义改变量,则在使用该容器的时候powershell会提示字符串不合法的错误)

ps c:\> start-container $container
ps c:\> get-container | start-container(启动所有的容器)

ps c:\> enter-pssession -containername tst -runasadministrator(使用powershell用户名为administrator的用户进入名为tst的容器,持久性powershell会话,还可以使用一次性操作容器)
ps c:\> invoke-command -containername tst -scriptblock {new-item -itemtype directory -path C:\application} (该命令为一次性在tst容器中在C盘下创建一个application的目录。)
ps c:\> stop-container $container
ps c:\> get-container | stop-container (停止所有容器)
ps c:\> remove-container $container -force
ps c:\> get-container | remove-container -force

以上命令使用powershell创建、使用、删除容器。具体的命令使用请get-help command

由于用习惯了linux的操作,在windows上一样的喜欢使用command-line的方式。

以下是使用docker的方式创建、使用、删除容器(具体的docker方式请help)。

PS C:\> docker images
C:\> docker run --name iisbase -it windowsservercore cmd
C:\> powershell.exe Install-WindowsFeature web-server
C:\> exit
C:\> docker commit iisbase windowsservercoreiis
C:\> docker images

if (!(Get-NetFirewallRule | where {$_.Name -eq "TCP80"})) {
New-NetFirewallRule -Name "TCP80" -DisplayName "HTTP on TCP/80" -Protocol tcp -LocalPort 80 -Action Allow -Enabled True
}

C:\> docker run --name iisdemo -it -p 80:80 windowsservercoreiis cmd

C:\> del C:\inetpub\wwwroot\iisstart.htm

PS C:\> docker run -p 80:80 windowsservercoreiis
PS C:\> docker stop tender_panini
PS C:\> docker stop $(docker ps -q)
PS C:\> docker rm prickly_pike
PS C:\> docker rm $(docker ps -a -q)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: