windows10/2016上使用docker

該文章是在一個物理主機使用windows server 2016或windows 10上使用docker非hyper-v虛擬機。

首先搭建一個容器主機。


按照微軟的官方文檔搭建該容器主機,步驟如下:


  1. 安裝容器功能

    PS C:\> start-process powershell -verb runas

    PS C:\> install-windowsfeature containers

    PS C:\> shutdown -r -t 0

    PS C:\> get-containerhost

  2. 啓用hyper-v角色

    PS C:\> install-windowsfeature hyper-v

  3. 創建虛擬交換機

    PS C:\> new-vmswitch -name "DHCP" -switchtype external(這是使用的橋接方式,還可以使用nat方式)

    PS C:\> new-vmswitch -name "virtual switch" -switchtype nat -natsubnetaddress 172.16.0.1/24

  4. 如果虛擬交換機配置的是nat類型,則需要創建nat對象。

    PS C:\> new-netnat -name containernat -internalipinterfaceaddressprefix "172.16.0.1/24"

  5. 安裝操作系統鏡像

    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

  6. 然後按照該文檔在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)













發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章