DockerでWindowsコンテナを使うためにはホストOSがWindowsである必要がある。
そもそもWindowsコンテナを必要とする人がWindowsを使っていることが珍しいと思うのですが、どうなんでしょう?
ということで、タイトルにある通りLinux上でもWindowsコンテナを使うことができないかと調べた結果、次のようなリポジトリを発見したので試していこうと思います。
コレ自体はMacOS上で利用することを想定していると思うのですが、Linux(Ubuntu)でも行けるはず。。。
github.com
動作イメージとしては、VirtualBox上のWindowsにインストールされたDockerにホストOS(Linux)から接続を行い、Windows上で動いているWindowsコンテナにアクセスするそんな感じです。
環境
- Ubuntu 22.04 LTS
- VirtuallBox 6.1.34
- Vagrant 2.2.19
- Packer 1.6.1(必要に応じて)
※StefanSchererさんがVagrant Boxを用意してくれているので、実施しなくても問題ありません
※M1 Macでも動くWindowsが必要な場合は実施してもらえればです
Packerでビルドを行い、Windows Server 2019のVagrant Boxイメージを作成します。(Windows isoファイルが大きいので1時間以上かかります)
github.com
$ packer build --only=virtualbox-iso windows_2019_docker.json
しばらくするとVirtualBoxが起動してWindowsのインストールが始まると思います。DockerをインストールしたりWindowsUpdateを行ったりと色々行ってくれます。

windows_2019_docker_virtualbox.box
というファイルが作られます。
Vagrantを起動し、Docket contextでWindowsコンテナに切り替える
続いて次のリポジトリをCloneしてVagrant Boxを起動させます。
github.com
$ vagrant up --provider virtualbox 2019-box
Bringing machine '2019-box' up with 'virtualbox' provider...
==> 2019-box: Box 'StefanScherer/windows_2019_docker' could not be found. Attempting to find and install...
2019-box: Box Provider: virtualbox
2019-box: Box Version: >= 0
==> 2019-box: Loading metadata for box 'StefanScherer/windows_2019_docker'
2019-box: URL: https://vagrantcloud.com/StefanScherer/windows_2019_docker
==> 2019-box: Adding box 'StefanScherer/windows_2019_docker' (v2021.05.15) for provider: virtualbox
2019-box: Downloading: https://vagrantcloud.com/StefanScherer/boxes/windows_2019_docker/versions/2021.05.15/providers/virtualbox.box
==> 2019-box: Box download is resuming from prior download progress
==> 2019-box: Successfully added box 'StefanScherer/windows_2019_docker' (v2021.05.15) for 'virtualbox'!
==> 2019-box: Preparing master VM for linked clones...
2019-box: This is a one time operation. Once the master VM is prepared,
2019-box: it will be used as a base for linked clones, making the creation
2019-box: of new VMs take milliseconds on a modern system.
==> 2019-box: Importing base box 'StefanScherer/windows_2019_docker'...
==> 2019-box: Cloning VM...
==> 2019-box: Matching MAC address for NAT networking...
==> 2019-box: Checking if box 'StefanScherer/windows_2019_docker' version '2021.05.15' is up to date...
==> 2019-box: Setting the name of the VM: windows-docker-machine_2019-box_1653714059442_57360
==> 2019-box: Clearing any previously set network interfaces...
==> 2019-box: Preparing network interfaces based on configuration...
2019-box: Adapter 1: nat
2019-box: Adapter 2: hostonly
==> 2019-box: Forwarding ports...
2019-box: 3389 (guest) => 3389 (host) (adapter 1)
2019-box: 5985 (guest) => 55985 (host) (adapter 1)
2019-box: 5986 (guest) => 55986 (host) (adapter 1)
2019-box: 22 (guest) => 2222 (host) (adapter 1)
==> 2019-box: Running 'pre-boot' VM customizations...
==> 2019-box: Booting VM...
==> 2019-box: Waiting for machine to boot. This may take a few minutes...
2019-box: WinRM address: 127.0.0.1:55985
2019-box: WinRM username: vagrant
2019-box: WinRM execution_time_limit: PT2H
2019-box: WinRM transport: negotiate
==> 2019-box: Machine booted and ready!
==> 2019-box: Checking for guest additions in VM...
==> 2019-box: Configuring and enabling network interfaces...
==> 2019-box: Mounting shared folders...
2019-box: /home/chichi1091 => /home/chichi1091
==> 2019-box: Running provisioner: shell...
2019-box: Running: scripts/create-machine.ps1 as C:\tmp\vagrant-shell.ps1
VirtualBoxを起動してみるとWindowsが起動していることがわかります。

$ docker context ls
NAME DESCRIPTION DOCKER ENDPOINT KUBERNETES ENDPOINT ORCHESTRATOR
2019-box 2019-box windows-docker-machine tcp://192.168.59.90:2376
default * Current DOCKER_HOST based configuration unix:///var/run/docker.sock swarm
Docker Engineにも追加されています。ではWindows上で動いているDockerに切り替えを行います。
$ docker context use 2019-box
2019-box
Current context is now "2019-box"
$ docker version
Client: Docker Engine - Community
Version: 20.10.16
API version: 1.41
Go version: go1.17.10
Git commit: aa7e414
Built: Thu May 12 09:17:30 2022
OS/Arch: linux/amd64
Context: 2019-box
Experimental: true
Server: Docker Engine - Community
Engine:
Version: 20.10.6
API version: 1.41 (minimum version 1.24)
Go version: go1.13.15
Git commit: 8728dd2
Built: Fri Apr 9 22:45:40 2021
OS/Arch: windows/amd64
Experimental: false
OS/Arch
がwindows/amd64
になっています!これでLinux Dockerが Windows Docker Engineとつながっている状態です。
元の状態に戻す場合は以下のコマンドを実行してください。
$ docker context use default
以下のコマンドを実行することで、VirtualBoxのWindowsで起動しているWindowsコンテナに接続することができます。
$ docker run -it -v C:$(pwd):C:$(pwd) mcr.microsoft.com/windows/servercore:1809 powershell
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.
PS C:\>
PowerShellが起動しました!しかもホストOSのディレクトリもちゃんとマウントされています。
VirtualBoxのWindowsにいろいろなWindowsコンテナを入れておけばLinuxからも操作することができます。予想される利用方法はiisが多いのかな?開発はLinuxやMacで行って動かすのはiisというのは意外とありそうな気がします。
M1 Macの場合はVirtualBoxが利用できないので変わりにParallelsを使えばイケそうな感じですね。
README.mdに書いてある内容をそのまま行ってみましたが、いろいろな可能性を秘めていて楽しそうです。