LCOW —— 單一Docker引擎下可同時運行Linux和Windows容器啦!

來源:互聯網
上載者:User

標籤:自動更新   服務   machine   run   ide   容器   rsh   分享圖片   --   

 79251059

 

就在上周,Docker官方的master分支上新增了LCOW(Linux Containers on Windows)[1]功能。啟用這項功能,即可在單一的Docker引擎下,同時運行Linux和Windows容器。
下面趕緊跟小編一起,看看Windows 10將會發生哪些變化?


  • 可以用Docker命令docker ps,列出所有正在啟動並執行Linux或Windows容器。

  • 在容器和主機之間通過儲存卷共用資料。

  • 容器之間可以通過容器網路互相通訊。

  • 通過將連接埠映射到主機,實現本地訪問。但目前,它還只是Windows 10 1803版預覽體驗計劃(Windows Insider)的一項功能。

 運行Linux容器

 

現在,你需要指定--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容器通過主機的一個共用資料夾,實現資料共用。


首先,在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:\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測試環境

 

如果你想試著玩一下LCOW,建議使用Windows 10 1709虛擬機器。
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: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
 建立Docker
現在,你可以下載並安裝Docker引擎了。
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環境

 

如果你的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 CodeJust-in-Time 偵錯你的應用程式,等等。
如果您喜歡這篇文章,請分享給您的朋友。如果想瞭解Windows容器的最新情況,請關注原文作者的推特@stefscherer[3]。
相關連結:
  1. https://github.com/moby/moby/pull/34859

  2. https://github.com/linuxkit/lcow/releases

  3. https://twitter.com/stefscherer

 

原文連結:https://stefanscherer.github.io/sneak-peek-at-lcow/

 

LCOW —— 單一Docker引擎下可同時運行Linux和Windows容器啦!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.