LoginSignup
1
0

More than 1 year has passed since last update.

Docker で Python の実行環境を作る

Last updated at Posted at 2022-05-16

ディレクトリ構成

./src/test.py が Docker の環境で実行させたいプログラムです。

.
├── Dockerfile
├── Makefile
├── docker-compose.yml
├── requirements.txt
└── src
    └── test.py

Dockerfile

Dockerfile
FROM python:3
USER root
WORKDIR /root/src/
ADD requirements.txt /root/src/
RUN pip install --upgrade pip
RUN pip install -r requirements.txt

docker-compose.yml

docker-compose.yml
version: '3'
services:
  python3:
    build: .
    container_name: 'python3'
    tty: true
    volumes:
      - ./src:/root/src

requirements.txt

使いたいパッケージを列挙します。

requirements.txt
numpy
matplotlib

コマンド

次の3つのコマンドでテストプログラムが実行できます。

$ docker-compose build
$ docker-compose up -d
$ docker exec -it python3 python test.py

Makefile

Docker 周りのコマンドは長ったらしいので、make で簡略化します。

Makefile
all:
	docker exec -it python3 python test.py
build:
	docker-compose build --no-cache
install:
	docker-compose build
up:
	docker-compose up -d
ps:
	docker-compose ps
version:
	docker exec -it python3 python --version
down:
	docker-compose down
bash:
	docker-compose exec python3 bash
1
0
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
1
0