LoginSignup
4
6

More than 5 years have passed since last update.

Docker Windows Container で PowerShell を実行する

Posted at

Docker Windows Container で PowerShell を実行する

やってみた時のメモ。

参考資料

環境

Host

WindowsServer2016 になります。

PS C:\Windows\system32> Get-WmiObject Win32_OperatingSystem


SystemDirectory : C:\Windows\system32
Organization    : Amazon.com
BuildNumber     : 14393
RegisteredUser  : EC2
SerialNumber    : 00376-40000-00000-AA753
Version         : 10.0.14393

やってみる

以下を参考できるかやってみます。

Windows コンテナを Compute Engine で動かす

PS C:\Windows\system32> docker pull microsoft/powershell:6.0.1-nanoserver-1709
6.0.1-nanoserver-1709: Pulling from microsoft/powershell
a Windows version 10.0.16299-based image is incompatible with a 10.0.14393 host

早速エラー。
これは Host と pull するイメージが合っていない為にエラー。

これは以下に対応表がある。

Windows Container Version Compatibility

今回、Host OS が「Windows Server 2016 Builds: 14393.」にも関わらず、コンテナOSが「Windows Server version 1709 Builds 16299.」を選択しようとしてエラーとなった。

microsoft/nanosserer には Supported Windows Server 2016もあるのでこちらを利用する。

PS C:\Windows\system32> docker pull microsoft/nanoserver

成功。
次にブログの記事にあるようにコンテナ内で PowerShell スクリプトを作成して docker commitして確認する

# powershell commnad を指定しないと cmd がデフォルトで起動して、Add-Content が使えなかった
PS C:\Windows\system32> docker run -it microsoft/nanoserver powershell

Add-Content C:\Users\Public\helloworld.ps1 'Write-Host "Hello World"'

exit

# 変更を commit してイメージを作る
PS C:\Windows\system32>docker commit 5793026e326e poyo/helloworld
sha256:7d3b6bdb33202b100ae79974e985626cc4ad2768f6004c0d02f187af157b94d5

# 実行してみる
PS C:\Windows\system32> docker run --rm poyo/helloworld powershell c:\Users\Public\helloworld.ps1
Hi

OK.
次に Dockerfile を作る

FROM microsoft/nanoserver
COPY hogefuga.ps1 C:/Users/Public/

これをビルド

PS C:\Windows\system32> d docker build -t hogefuga/poyo .
ERRO[0000] Can't add file \\?\C:\Users\Administrator\AppData\Local\Application Data to tar: readlink \\?\C:\Users\Admini
・・・

エラーになる。

Build with PATH

This example specifies that the PATH is ., and so all the files in the local directory get tard and sent to the Docker daemon.

なるほど。
特にフォルダなどを作らずビルドしていたので、ローカルディレクトリのファイル群を送信しようとして問題になったようにみえる。
任意のフォルダを作成し、Dockerfile と ps1 ファイルのみであればビルドに成功した。

PS C:\Windows\system32> docker run -d hogefuga/poyo powershell c:\Users\Public\hogefuga.ps1
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6