すくすくすくらっぷ

とりあえずすくらっぷ

DockerでPythonの環境構築を行う(Windows10 Home編)

前回はMac上にDockerの環境構築を行い、DockerFileからPython3.x系が実行できるコンテナを作成しました。

今回は、Windows10 Homeのマシン上に同じくDockerの環境構築を行い、Dockerfileを使ってPython3.x系が実行できるコンテナの作成を行います。

Dockerのインストール

Windows 10 Homeの場合だとDocker for Windows(Hyper-V)が利用できないため、 代わりにDocker Toolboxをインストールします。

Windows 10 Homeな方はこちらから
https://github.com/docker/toolbox/release

インストーラの通り進めると、3つのアイコンがデスクトップに現れます。 f:id:blauthree:20190727232704p:plain

一番下の「Docker Quickstart Terminal」をダブルクリックします。

何度か変更の許可を求めるダイアログが出てくるので「許可」を選択します。

しばらく待つと下のような画面になり、Dockerコマンドが使用可能になります。 f:id:blauthree:20190727233402p:plain

$ docker version
Client:
 Version:           18.09.3
 API version:       1.39
 Go version:        go1.12
 Git commit:        774a1f4eee
 Built:             Mon Mar  4 10:36:44 2019
 OS/Arch:           windows/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.0
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.5
  Git commit:       aeac9490dc
  Built:            Wed Jul 17 18:22:15 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.2.6
  GitCommit:        894b81a4b802e4eb2a91d1ce216b8817763c29fb
 runc:
  Version:          1.0.0-rc8
  GitCommit:        425e105d5a03fabd737a126ad93d62a9eeede87f
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

次に、前回作成したDockerfileを使ってイメージとコンテナを作成します。

Dockerfileがある場所まで移動し、以下のコマンドを実行します。

$> docker build .
Sending build context to Docker daemon  2.048kB
Step 1/4 : FROM ubuntu:18.04
18.04: Pulling from library/ubuntu
5b7339215d1d: Pull complete
14ca88e9f672: Pull complete
a31c3b1caad4: Pull complete
b054a26005b7: Pull complete
Digest: sha256:9b1702dcfe32c873a770a32cfd306dd7fc1c4fd134adfb783db68defc8894b3c
Status: Downloaded newer image for ubuntu:18.04
 ---> 4c108a37151f
Step 2/4 : RUN apt-get update
 ---> Running in 65165292f804
Get:1 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic InRelease [242 kB]
Get:3 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [725 kB]
...
Requirement already satisfied: six in /usr/lib/python3/dist-packages (from cycler>=0.10->matplotlib)
Requirement already satisfied: setuptools in /usr/lib/python3/dist-packages (from kiwisolver>=1.0.1->matplotlib)
Installing collected packages: numpy, cycler, python-dateutil, pyparsing, kiwisolver, matplotlib
Successfully installed cycler-0.10.0 kiwisolver-1.1.0 matplotlib-3.1.1 numpy-1.16.4 pyparsing-2.4.1 python-dateutil-2.8.0
Removing intermediate container 3deddb4c1414
 ---> b6337a07e65e
Successfully built b6337a07e65e
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

下のほうにWindowsからWindows以外のホストを作成すると、パーミッションがこんな感じになるよ的な警告が書かれていますがそのまま続けます。

作ったイメージをdocker imageで確認します。

$> docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
<none>              <none>              b6337a07e65e        9 minutes ago       605MB
ubuntu              18.04               4c108a37151f        4 weeks ago         64.2MB

IMAGE IDが「b6337a07e65e」のものが作成したイメージになります。

イメージには特に名前がないためIMAGE IDで指定してコンテナを作成します。

$> docker run -it -d --name ubuntu_py3 b6337a07e65e
ae058a43a5529f26ac1b48aa289e81807dc39e51f35421dc174b45017014d63b
$> docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
ae058a43a552        b6337a07e65e        "/bin/bash"         50 seconds ago      Up 45 seconds                           ubuntu_py3

指定した名前でコンテナができました。コンテナの中に入ります。

$> docker exec -it ubuntu_py3 /bin/bash
root@ae058a43a552:/#
root@ae058a43a552:/# python3
Python 3.6.8 (default, Jan 14 2019, 11:02:34)
[GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import numpy
>>> import matplotlib
>>> exit()
root@ae058a43a552:/#

前回と同様にPythonの入ったコンテナを作成することができました。