LoginSignup
1
0

More than 1 year has passed since last update.

Docker Composeを使ったPython環境の用意

Posted at

コンテナでPython環境を用意したい!けど調べるの面倒なんで備忘録としてまとめます。

構成

.
├── Dockerfile
├── docker-compose.yaml
└── opt
    └── test.py

Dockerfileの定義

cat <<EOF > Dockerfile
FROM python:3
USER root
RUN apt-get update
RUN apt-get install -y vim 
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
EOF

docker-compose.yamlの定義

cat << EOF > docker-compose.yaml
version: '3'
services:
  python3:
    restart: always
    build: .
    container_name: 'python'
    working_dir: '/root/opt/'
    tty: true
    volumes:
      - ./opt:/root/opt
EOF

コンテナの立ち上げ

sudo docker-compose up -d --build

動作確認

$ sudo docker ps
CONTAINER ID   IMAGE            COMMAND     CREATED        STATUS       PORTS     NAMES
0cf496a155c1   python_python3   "python3"   9 seconds ago  Up 8 seconds             python

test.pyの実行

コンテナに入る

sudo docker-compose exec python3 bash

test.pyの作成

cat <<EOF > test.py
print("Hello, Python")
EOF

test.pyの実行

# python3 test.py
Hello, Python

参考サイト

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