LoginSignup
14
25

More than 3 years have passed since last update.

【Mac】Docker を使って最速で Python 3.x 環境を構築する

Last updated at Posted at 2019-12-01

Mac は標準で Python 2.7 がインストールされていますので、Python 3.x の環境が必要な場合は、Pipenvpyenv を使って環境構築することが多いと思います。

しかし、インストールに失敗したり、設定に手間取ったりすることが多かったので、Docker を使った Python 3.x 環境の構築方法をまとめておきます。

動作環境

  • Mac OS X 10.14.6

Docker を使って Python 3.x 環境を構築

以下の URL から「Docker Desktop for Mac」をダウンロードし、インストールします。
https://hub.docker.com/editions/community/docker-ce-desktop-mac

Docker のインストールが完了したら、「docker version」コマンドを実行し、正しくインストールされているか確認してみましょう。

$ docker version
Client: Docker Engine - Community
 Version:           19.03.5
 API version:       1.40
 Go version:        go1.12.12
 Git commit:        633a0ea
 Built:             Wed Nov 13 07:22:34 2019
 OS/Arch:           darwin/amd64
 Experimental:      false

Server: Docker Engine - Community
 Engine:
  Version:          19.03.5
  API version:      1.40 (minimum version 1.12)
  Go version:       go1.12.12
  Git commit:       633a0ea
  Built:            Wed Nov 13 07:29:19 2019
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          v1.2.10
  GitCommit:        b34a5c8af56e510852c35414db4c1f4fa6172339
 runc:
  Version:          1.0.0-rc8+dev
  GitCommit:        3e425f80a8c931f88e6d94a8c831b9d5aa481657
 docker-init:
  Version:          0.18.0
  GitCommit:        fec3683

任意のフォルダを Python のプロジェクトフォルダとして作成し、そのフォルダ内に Python のソースファイルを配置します。
※この記事では、「/Users/username/work/python/helloworld」をプロジェクトフォルダとして使用します。

$ cd /Users/username/work/python/helloworld
$ echo 'print("Hello World!")' > helloworld.py

「docker run」コマンドを実行し、Python の実行環境となるコンテナを起動します。

以下のオプションを指定します。

  • -i (--interactive) : コンテナの標準入力にアタッチします。
  • -t (--tty) : 疑似ターミナル (pseudo-TTY) を割り当てます。
  • --rm : コンテナ終了時に、自動的にコンテナを削除します。
  • -v (--volume) : ローカルのフォルダをコンテナ上にマウントします。(<ローカル上のフォルダ>:<コンテナ上のフォルダ>)

Docker イメージは、Docker Hub で公開されている「python:3.7-alpine」を使用します。

コマンド末尾の「/bin/sh」は、コンテナ起動時に実行されるコマンドです。

$ docker run -it --rm -v /Users/username/work/python/helloworld:/helloworld python:3.7-alpine /bin/sh

コンテナが起動したら、以下のコマンドを実行して、正しく動作しているか確認してみましょう。

/ # ls
bin         etc         home        media       opt         root        sbin        sys         usr
dev         helloworld  lib         mnt         proc        run         srv         tmp         var
/ # python --version
Python 3.7.5

プロジェクトフォルダに移動し、Python ファイルを実行します。

/ # cd helloworld/
/helloworld # python helloworld.py 
Hello World!

「exit」コマンドを実行すると、コンテナを終了します。

/helloworld # exit

コンテナ起動時にパッケージをインストール

コンテナは起動時に初期化されるので、起動毎に必要なパッケージをインストールする必要があります。

それでは面倒なので、新しい Docker イメージを作成し、コンテナ起動時に必要なパッケージがインストールされるようにします。

プロジェクトフォルダ内に Dockerfile を作成します。

Dockerfile
# ベースとなる Docker イメージを指定
FROM python:3.7.5-alpine

# プロジェクトフォルダを指定
ARG project_dir=/helloworld/

# requirements.txt をコンテナにコピー
ADD requirements.txt $project_dir

# requirements.txt に書かれたパッケージをインストール
WORKDIR $project_dir
RUN pip install -r requirements.txt

「docker build」コマンドを実行し、新しい Docker イメージ「helloworld」を作成します。

$ docker build -t helloworld .

新しく作成した Docker イメージ「helloworld」を指定して「docker run」コマンドを実行すると、コンテナ起動時に requirements.txt に書かれたパッケージがインストールされます。

$ docker run -it --rm -v /Users/username/work/python/helloworld:/helloworld helloworld /bin/sh
14
25
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
14
25