LoginSignup
23
28

More than 1 year has passed since last update.

DockerでPython3 - 実務編

Last updated at Posted at 2021-07-26

ひとしきり、最低限の手順です。

準備

Docker for windows desktop等 (記事末尾参照)

├ Readme.md
├ Dockerfile
├ docker-compose.yml
└ opt
    ├ foo.py
    └ bar.py

docker-compose.yml

docker-compose.yml
ja: https://docs.docker.jp/compose/compose-file.html
en: https://docs.docker.com/compose/

docker-compose.yml
services: # アプリケーションを動かすための各要素
  python3: # 任意の名称
    restart: always # 実行時に再起動するかどうか
    build: . # ComposeFileを実行し、ビルドされるときのpath
    container_name: 'python3' # コンテナ名
    working_dir: '/root/'
    tty: true # docker-compose up したコンテナを起動させ続ける
    volumes:
      - ./opt:/root/opt

Dockerfile

Dockerfile
https://hub.docker.com/_/python

FROM python:3 # ベースイメージの指定 (https://hub.docker.com/_/python Create a Dockerfile in your Python app project 参照)
USER root
RUN apt-get update
RUN apt-get -y install locales && \
    localedef -f UTF-8 -i ja_JP ja_JP.UTF-8
ENV LANG ja_JP.UTF-8
ENV LANGUAGE ja_JP:ja
ENV LC_ALL ja_JP.UTF-8
ENV TZ JST-9
ENV TERM xterm
RUN apt-get install -y vim less
RUN pip install --upgrade pip
RUN pip install --upgrade setuptools
RUN pip install psycopg2
RUN pip install svn
RUN pip install requests

VSCode

【VS Code】Dockerコンテナの環境でリモート開発【Win/Mac】 | チグサウェブ

拡張機能 Remote-Development をインストール
Dockerfile のある階層を選択する

Python 参考

コンテナ起動

docker-compose up -d --build

コンテナへ接続

docker-compose exec python3 bash
docker exec python3 python ./opt/foo.py

installされているものを確認してみる

root@c75334aa5328:~/opt# python -m pip list
Package            Version
------------------ ---------
certifi            2021.5.30
charset-normalizer 2.0.3
idna               3.2
nose               1.3.7
pip                21.1.3
psycopg2           2.9.1
python-dateutil    2.8.2
requests           2.26.0
setuptools         57.2.0
six                1.16.0
svn                1.0.1
urllib3            1.26.6
wheel              0.36.2

Python プログラム実行

python3 foo.py など。

Enjoy!

コンテナが要らなくなったら...

下記で削除する。

docker-compose down

参考

Docker Desktop

Docker基本

docker-compose.yml

DockerとPython

以上参考になればさいわいです。

23
28
2

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
23
28