しばたテックブログ

気分で書いている技術ブログです。

PowerShell on LinuxをDocker上で試す

試した系の記事は既に多くの方がやっていますが、自分も一つくらいは書いとかないといけないかなと思ったので。

インストール手順

ぶっちゃけインストール手順はGitHubに載ってるのでそれを見れば十分です。
実のところ、わざわざこんなエントリ書く必要も無いのですが、そこはご愛敬ということでお許しくださいw

https://github.com/PowerShell/PowerShell/blob/master/docs/installation/docker.mdgithub.com

【2016/09/19変更】

PowerShell 6.0.0.Alpha10からDockerのインストール手順がガラッと変わってしまいました...
Alpha9では単純にサンプルとなるDockerfileだけ公開されていたのですが、いまはlaunch.shによりディストリビューションの選択、安定版や非安定版を設定することができる様になっています。

github.com

以降の内容はAlpha9から更新しませんが、Ubuntuで最低限のビルドを行う手順だ捉えていただければ、と思います。

PowerShell on LinuxをDocker上で試す

実行環境

今回試す環境は以下。

  • 64bit版 Windows 10 Pro RS1(1607)
  • Docker for Windows 1.12

GitHub上にあるDockerfileはUbuntuイメージをベースにしてるのでDocker for Windowsを使いました。
WindowsでなくてもLinuxイメージを扱える環境であれば何でも良いです。

Dockerfile

(Alpha9時点の)Dockerfileの内容はこんな感じ。
ベースイメージに対してPowerShell on Linuxのインストール手順をひとつづつRUNしてるだけなのでとても分かりやすいですね。

FROM ubuntu:16.04

RUN apt-get update && apt-get install -y curl libunwind8 libicu55 libcurl3
RUN curl -LO https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.9/powershell_6.0.0-alpha.9-1ubuntu1.16.04.1_amd64.deb
RUN dpkg -i powershell_6.0.0-alpha.9-1ubuntu1.16.04.1_amd64.deb

Entrypoint [ "powershell" ]

※補足として最後により新しいDockerfileを記載しておきますのでそちらも参考にしてください。

docker build

このDockerfileを適当なディレクトリに配置してdocker buildします。
以下の例はC:\TempにDockerfileを置いた場合です。

cd C:\Temp\
docker build -t powershell .

実行結果はこんな感じです。
各Stepにエラーが無ければOKです。

PS C:\Temp> docker build -t powershell .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu:16.04
16.04: Pulling from library/ubuntu

952132ac251a: Pull complete
82659f8f1b76: Pull complete
c19118ca682d: Pull complete
8296858250fe: Pull complete
24e0251a0e2c: Pull complete
Digest: sha256:f4691c96e6bbaa99d99ebafd9af1b68ace2aa2128ae95a60369c506dd6e6f6ab
Status: Downloaded newer image for ubuntu:16.04
 ---> bd3d4369aebc
Step 2 : RUN apt-get update && apt-get install -y curl  libunwind8 libicu55
 ---> Running in 19bfb1cee4a8
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]

・・・ (中略) ・・・

Updating certificates in /etc/ssl/certs...
173 added, 0 removed; done.
Running hooks in /etc/ca-certificates/update.d...
done.
 ---> c43bd397c1af
Removing intermediate container 19bfb1cee4a8
Step 3 : RUN curl -LO https://github.com/PowerShell/PowerShell/releases/download/v6.0.0-alpha.9/powershell_6.0.0-alpha.9-1ubuntu1.16.04.1_amd64.deb
 ---> Running in b93ca6e75e85
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   628    0   628    0     0    340      0 --:--:--  0:00:01 --:--:--   340
100 39.0M  100 39.0M    0     0  82054      0  0:08:18  0:08:18 --:--:-- 90143
 ---> f1d271970597
Removing intermediate container b93ca6e75e85
Step 4 : RUN dpkg -i powershell_6.0.0-alpha.9-1ubuntu1.16.04.1_amd64.deb
 ---> Running in 9eefe54aa684
Selecting previously unselected package powershell.
(Reading database ... 7831 files and directories currently installed.)
Preparing to unpack powershell_6.0.0-alpha.9-1ubuntu1.16.04.1_amd64.deb ...
Unpacking powershell (6.0.0-alpha.9-1) ...
Setting up powershell (6.0.0-alpha.9-1) ...
 ---> b3ae5c7c67e8
Removing intermediate container 9eefe54aa684
Step 5 : ENTRYPOINT powershell
 ---> Running in 678e61405749
 ---> 017f3d9a7bd0
Removing intermediate container 678e61405749
Successfully built 017f3d9a7bd0
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context wil
l have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

結果の確認

docker imageコマンドで作成したイメージを確認すると以下の様になり、powershellが出来ていることがわかります。

PS C:\Temp> docker images
REPOSITORY                            TAG                 IMAGE ID            CREATED             SIZE
ubuntu                                16.04               bd3d4369aebc        9 hours ago         126.6 MB
powershell                            latest              017f3d9a7bd0        14 hours ago        381.6 MB

docker run

docker runコマンドで作成したイメージからコンテナを起動します。
-itオプションで対話モードでpowershellを起動します。

docker run -it powershell

実行した結果は以下の様になります。

f:id:stknohg:20160828125714p:plain

ホスト環境がWindowsなので若干カーソルの挙動に怪しいところがありますがお試しで使うに分には十分です。
これで気軽にPowerShell on Linuxを試せますね。

おまけ

海外のMVPの方がAlpine LinuxでPowerShellを動かせないか画策している様です。

github.com

Alpine Linuxで動かすことができればベースイメージのサイズも減りますしDockerでPowerShell試すのがもっと気軽になりそうですね。

最新のDockerfile

【2016/09/19 追記】
【2016/10/19 更新】

現時点(alpha11)でのUbuntu16.04安定版のDockerfileの内容です。
本エントリの内容はこちらのDockerfileでも試して動作を確認しています。

FROM ubuntu:xenial
MAINTAINER Andrew Schwartzmeyer <andschwa@microsoft.com>

ARG POWERSHELL_RELEASE=v6.0.0-alpha.11
ARG POWERSHELL_PACKAGE=powershell_6.0.0-alpha.11-1ubuntu1.16.04.1_amd64.deb

# Setup the locale
ENV LANG en_US.UTF-8
ENV LC_ALL $LANG
RUN locale-gen $LANG && update-locale

# Install dependencies and clean up
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        libc6 \
        libcurl3 \
        ca-certificates \
        libgcc1 \
        libicu55 \
        libssl1.0.0 \
        libstdc++6 \
        libtinfo5 \
        libunwind8 \
        libuuid1 \
        zlib1g \
        curl \
        git \
    && rm -rf /var/lib/apt/lists/*

# Install PowerShell package and clean up
RUN curl -SLO https://github.com/PowerShell/PowerShell/releases/download/$POWERSHELL_RELEASE/$POWERSHELL_PACKAGE \
    && dpkg -i $POWERSHELL_PACKAGE \
    && rm $POWERSHELL_PACKAGE

# Use array to avoid Docker prepending /bin/sh -c
ENTRYPOINT [ "powershell" ]