
Docker for Windows Beta, 操作體驗大躍進!
開始 step by step 之前,先講一下我對這版的感想。不想聽這段心得的話,可以直接跳到後面看如何在 VM 裡面體驗這次大改版的 Docker for Windows beta... 要在 VM 裡面跑這個有些小地雷要閃過,我就順手整理在後面了。 做這些基礎建設的公司,真的都要掌握自己的虛擬化技術才玩得起來,不然就是要密切的跟掌握這些技術的公司合作才行。這次 Docker for Windows 我想背後 Microsoft 應該幫了不少忙吧? 不但跟 Hyper-V 整合的天衣無縫,同時 Windows Container 的幾個關鍵 Feature 也開始看的到跡象了,背後雙方應該合作了很久,現在開始端出成果出來了。我就被之前 Virtual Box 搞到完全不想在 Windows 上面跑 Docker, 索性忘掉 Docker Toolbox for Windows, 直接自己開 Linux 的 VM,不然就是直接找獨立的機器裝 Linux + Docker Engine, 用 SSH 連進去玩。完全跟我自己的 Host OS (Windows 10 Ent) 沒有直接的關聯,自然也沒甚麼整合性可言。換句話說,你不會 Linux 就不用玩了...。 畢竟,現在的 Container 都還是以 Linux 為主,未來兩三年內年應該還是脫離不了得附掛 Linux VM 才能執行 Docker 的狀況。很尷尬的一點就是,Container 就是為了脫離 VM 才設計出來的輕量化架構,而現在 Windows 上要跑 Linux 的 Container 又非得使用 VM 不可,這些整合的問題就浮現出來了。Mac 雖然也是 Unix 系列的 OS,不過她畢竟也不是 Linux, 也有類似問題存在。如何在虛擬化技術及整合上做到完美,就是這次改版主要的訴求了。 Mac 版的我沒有研究,有很多前輩分享過這部份了,我就不班門弄斧了,我就針對 for windows 版本的來研究。這次在架構上,官網講到有幾項主要的改進,對我而言有感的改變有這幾個 (紅字): Docker for Mac and Docker for Windows are faster, more reliable alternatives to Docker Toolbox for running Docker on these platforms. Here are some highlights of the new products.- Faster and more reliable - No more VirtualBox! On Mac, the Docker Engine runs in an xhyve Virtual Machine (VM) on top of an Alpine Linux distribution. The VM is managed by the native Docker application. On Windows, the Docker Engine is running in a Hyper-V VM. You do not need Docker Machine (
docker-machine
) to run Docker for Mac and Docker for Windows. - Native apps for better tools integration - Docker for Mac and Docker for Windows are native applications, including native user interfaces and the ability to stay updated automatically. The Docker tools are bundled with these apps, including the Docker command line interface (CLI), Docker Compose, and Docker Machine.
- Use with Docker Toolbox and Docker Machine - Docker for Mac and Docker for Windows can be used at the same time as Docker Toolbox on the same machine. Docker for Mac and Docker for Windows do not include Kitematic yet. So, for now, the only reason you would still need both Toolbox and Docker for Mac or Windows is to run Kitematic. (See Docker for Mac vs. Docker Toolbox in the Mac documentation. A Windows-specific version is coming soon.)
- Volume mounting for your code and data - Volume data access is fast and works correctly.
- Support for multiple architectures out-of-the-box - Docker for Mac and Windows lets you build and run Docker images for Linux x86 and ARM. (See Leveraging Multi-Architecture Support in the Mac documentation. A Windows-specific version is coming soon.)
Tips: 如何在 VM 裡面體驗 Docker for Windows?
感想先寫到這邊,接下來我就把我在 VM 裡面測試 Docker for Windows beta 的過程簡單紀錄一下。這邊有些技巧要留意,這是官網沒有講的。因為 Docker Engine 還是得依靠 Linux, 因此背後藏一台 Linux VM 仍然是必要的作法。既然我要在 VM 裡測試 Docker for Windows beta, 那麼 VM 裡面要再開 VM 就是閃不了的議題了。還好之前這篇已經先演練過 Nested Hyper-V 了,架構上的問題 Microsoft 已經解決了,剩下就是規劃跟實作。這次我要示範的架構圖如下:STEP #1, 準備好支援 Nested Hyper-V 的 VM
這部分可以參考之前的文章 (如何在 VM 裡使用 Docker ToolBox ?),或是直接參考 Microsoft 的官方說明即可。這部分難在 Hyper-V Nested Support 還只是 preview 階段,可能你連想都沒想到能這樣解決,或是根本還不知道有這功能...。另外,Preview 階段的東西文件也不足,也還沒有 GUI 可以用,只能透過 PowerShell Script 來啟用他。同時啟用 Nested Hyper-V 的 VM,也有一些規格限制要留意。這些細節突破後就過關了。以下是操作步驟: 第一件事,按照上圖施工,當然是在 Host PC 上 (CHICKEN-PC) 先準備好你要執行 Docker for Windows beta 的 VM (WIN10)。CHICKEN-PC 必須符合 Nested Virtualization 的需求,按照 Microsoft 的說法,OS 必須是 Windows 10 Pro or Enterprise / Windows Server 2016 Build 10565 以上的版本。Windows 10 Home 因為不支援 Hyper-V, 直接淘汰出局...。 至於 WIN10 VM 跑的 OS 要能夠執行這版 Docker for Windows beta 的系統需求,又有點不同,官網寫的是:System Requirements for Docker for Windows Windows 10 Pro (1511 November update, Build 10586) and above, with Hyper-V package installed.
不過,我實際上用 Windows 10 Enterprise 也是 OK 的,Windows Server 2016 沒在列表上,我沒試過... 總之,我在 CHICKEN-PC 跟 WIN10 這兩個 Host 用的都是 Windows 10 Enterprise 10586 這版本,這組合是可以正確無誤的執行的。 同時,用到 Nested Virtualization 技術的 VM,會有這些限制,官方列了十幾條,這邊特別要留意的我標紅字:- Both hypervisors need to be the latest versions of Hyper-V. Other hypervisors will not work. Windows Server 2012R2, or even builds prior to 10565 will not work.
- Once nested virtualization is enabled in a VM, the following features are no longer compatible with that VM. These actions will either fail, or cause the virtual machine not to start if it is hosting other virtual machines:
- Dynamic memory must be OFF. This will prevent the VM from booting.
- Runtime memory resize will fail.
- Applying checkpoints to a running VM will fail.
- Live migration will fail — in other words, a VM which hosts other VMs cannot be live migrated.
- Save/restore will fail.Note: these features still work in the “innermost” guest VM. The restrictions only apply to the first layer VM.
- Once nested virtualization is enabled in a VM, MAC spoofing must be enabled for networking to work in its guests.
- Hosts with Device Guard enabled cannot expose virtualization extensions to guests. You must first disable VBS in order to preview nested virtualization.
- Hosts with Virtualization Based Security (VBS) enabled cannot expose virtualization extensions to guests. You must first disable VBS in order to preview nested virtualization.
- This feature is currently Intel-only. Intel VT-x is required.
- Beware: nested virtualization requires a good amount of memory. I managed to run a VM in a VM with 4 GB of host RAM, but things were tight.
STEP #2, 在 VM 內安裝設定 Docker for Windows Beta
接下來的動作,通通都轉移到 VM: WIN10 裡面進行了。如果你有申請 Beta Program, 你應該會收到下載網址,以及測試用的 Token.. 這段過程很無腦,下載安裝包 (MSI package), 下一步下一步按完就 OK 了:
STEP #3, 執行 Docker Container: Hello-World
這邊測試我就簡單扼要的進行就好。我會測試兩個 container, 一個就是 hello-world, 證明這樣的架構是可運作的,第二個就是直接開 shell, 來驗證 mount local volume 的機制是否會運作。 若你順利的進行完前述 STEP #1 及 #2 的步驟的話,這步驟就再簡單不過了,唯一的差別是,過去需要自己 SSH 連到 Linux VM 內執行 Docker CLI, 現在可以直接在 Windows 下,透過 DOS Command Prompt / PowerShell 來執行 Docker CLI 指令,省了一層轉換,好處是你可以更密切的跟原生的 Windows 批次檔做整合,去運用及操控這些 Docker 的資源。 延續前述的環境,在 WIN10 VM 環境下,開啟 DOS 命令提示字元,執行 docker run 指令:docker run --rm hello-world
STEP #4, 掛載 Windows Folder, 給 Container 使用
接下來的案例,是這篇的最後一個 DEMO,也是本篇的重頭戲: 要展示 Local Volume Mount 的機制。過去要嘛就是直接掛載 Linux VM 內的 volume, 需要溝通的話就開 SAMBA ,讓 Windows 透過網芳來存取。或是反過來 Windows 開分享,讓 Linux VM 用 CIFS mount 起來用。不管是哪一個方法,明明都在本機的 DISK,還要透過網路繞一圈 (雖然沒有真正繞出去啦) 實在是很蠢... Docker for Windows 開始有專屬的設定方式可以解決這個問題了,在 Settings 畫面中,[Share Drivers] 就可以讓你勾選那些 DISK 能夠允許 Docker Engine 來使用,這應該是透過 Docker Volume Driver 來實現的。設定很簡單,Local Disk 有勾起來的,將來就能用在 Docker Volume 上面。 不過,前面的 "蠢" 方法講太快... Docker for Windows 一樣是透過網路繞了一圈,只是他把動作做到最精簡而已。我做了個小實驗,我的 WIN10 VM 有兩顆硬碟,分別是 C:\ 跟 D:\: 只勾選 C,結果 windows 就多了 C 這個分享目錄:docker run -it --rm -v C:\Users\chicken\Docker\apline-data:/data apline /bin/ash沒有啟動 C:\ shared drivers 的情況下,並不會出錯,不過 container 內的確也看不到我預先放的 readme.txt 檔... 反過來測試看看,我在 apline linux 的 shell 下執行這段指令:
cp /proc/version /data/aplinux-version.txt
docker run -it --rm -v c:\Users\chicken\Docker\apline-data:/data apline /bin/ash
cp /proc/version /data/aplinux-version.txt cat /data/aplinux-version.txt
後記: Container Isolation Technonlgy
最後一個還沒機會測試的功能,也是我很感興趣的:Support for multiple architectures out-of-the-box - Docker for Mac and Windows lets you build and run Docker images for Linux x86 and ARM. (See Leveraging Multi-Architecture Support in the Mac documentation. A Windows-specific version is coming soon.)
Linux x86 這次就已經能執行了,ARM 我手邊則還沒有東西可以測試...。我比較有機會用到的是 Windows Container, 看來也在將來的支援範圍內。這只能從文件跟這次的 release 找出一些蛛絲馬跡了。 舉個例子,Microsoft 在 MSDN 曾經提到,Windows Container 的架構為了 Docker 的弱項 "isolation" 做了加強,除了常看到用 namespace 做隔離之外 (process isolation),Microsoft 也提供了更高隔離層級的 hyper-v container (kernel isolation)。簡介可以參考這篇 MSDN BLOG:There are two different kind of containers on Windows : Windows Container and Hyper-V Container. They work in the same way, instead of that Hyper-V containers are more isolated than Windows Container because they are running in a very lightweight virtual machine that provides kernel isolation and not just process isolation.
For more information about Hyper-V containers, check the official documentation on MSDN:https://msdn.microsoft.com/en-us/virtualization/windowscontainers/management/hyperv_container
看來 Microsoft 跟 Docker 的合作很密切啊,這版的 Docker CLI 就已經看的到這 options:docker run --help
Docker Command Line Reference: run / --isolation
Specify isolation technology for container (--isolation)
This option is useful in situations where you are running Docker containers on Microsoft Windows. The --isolation <value>
option sets a container’s isolation technology. On Linux, the only supported is the default
option which uses Linux namespaces. These two commands are equivalent on Linux:
$ docker run -d busybox top $ docker run -d --isolation default busybox top
On Microsoft Windows, can take any of these values:
Value | Description |
---|---|
default |
Use the value specified by the Docker daemon’s--exec-opt . If the daemon does not specify an isolation technology, Microsoft Windows uses process as its default value. |
process |
Namespace isolation only. |
hyperv |
Hyper-V hypervisor partition-based isolation. |
Hyper-V container
Create container
Managing Hyper-V containers with Docker is almost identical to managing Windows Server containers. When creating a Hyper-V container with Docker, the --isolation=hyperv
parameter is used.
docker run -it --isolation=hyperv nanoserver cmd
總結
寫到這邊,總算把我這幾天研究成果寫完,可以告一段落了。落落長的寫了一堆,實際操作的細節,可能在將來 release 推出時都還會異動,各位其實不大需要去記這些細節。 不過更重要的,是透過 beta 搶先了解有那些問題會在將來的版本被解決掉? 還有官方的發展趨勢是什麼? 像這次就看到 Docker 跟兩大主流 Desktop OS 的無縫整合越做越好了,目的我想就是要改善 DevOps 流程的前段 (Development)。 另一個主軸就是 Docker 跟 Microsoft 的合作越來越密切了,這其實是件好事,對我而言這代表:- Microsoft 的 Windows Container 將會跟 Docker 有很好的相容性,不論是在架構上,工具上,甚至 API 都是。
- Windows Container 除了 OS 的不同之外,Docker 在架構及流程上的好處都可以繼續沿用。
- Docker Hub 官方公開的 Registration 也率先支援了 Windows Container, 更確保將來 Deployment 的機制是一致且可以共用的。