LoginSignup
4
0

More than 1 year has passed since last update.

ちょっとしたPython実行環境 on Docker

Last updated at Posted at 2021-07-21

Pythonの実行環境が欲しいなあと思った

きっかけは、ULIDの文字列が欲しいのだけど、さくっと出してくれるのが見つからなかった
ならば、作ってしまえば良いってことで

環境


上記のを一部抜粋
FROM alpine:3.14

# ensure local python is preferred over distribution python
ENV PATH /usr/local/bin:$PATH

# http://bugs.python.org/issue19846
# > At the moment, setting "LANG=C" on a Linux system *fundamentally breaks Python 3*, and that's not OK.
ENV LANG C.UTF-8

# runtime dependencies
RUN set -eux; \
    apk add --no-cache \
# install ca-certificates so that HTTPS works consistently
        ca-certificates \
# and tzdata for PEP 615 (https://www.python.org/dev/peps/pep-0615/)
        tzdata \
    ;

・・・
・・・
・・・
CMD ["python3"]
  • Linux Debianで良い場合はお手軽に以下のように記述量は減らせる
FROM python:3

WORKDIR /usr/src/app

COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD [ "python", "./your-daemon-or-script.py" ]

devcontainer.json
// For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
// https://github.com/microsoft/vscode-dev-containers/tree/v0.187.0/containers/docker-existing-dockerfile
{
    "name": "Existing Dockerfile",

    // Sets the run context to one level up instead of the .devcontainer folder.
    "context": "..",

    // Update the 'dockerFile' property if you aren't using the standard 'Dockerfile' filename.
    "dockerFile": "../Dockerfile",

    "settings": {},

    "extensions": [],

    // Use 'forwardPorts' to make a list of ports inside the container available locally.
    // "forwardPorts": [],

    // Uncomment the next line to run commands after the container is created - for example installing curl.
    // "postCreateCommand": "apt-get update && apt-get install -y curl",

    // docker container name (Identify Dev Containers)
    "runArgs": [
        "--name=python3.9-alpine-dev_container"
    ]

}

コンテナを動かす

  1. VSCodeからDockerfileのあるディレクトリを開いて、Reopen in Containerで開く。参考は、 公式ドキュメント
  2. コンテナ初期化が始まる(少し時間かかる、数分
  3. VSCodeのターミナルで接続して、python --version 後は、.py や pythonコマンドライン使う
$ python --version
Python 3.9.6

$ cd /workspaces
$ python -m venv venv
$ . venv/bin/activate
$  pip install ulid-py
  • pythonコマンドライン使って
python
import ulid
id1 = ulid.new()
id1
id1.str

実行例

937f59a63c54:/workspaces/venv# python --version
Python 3.9.6
937f59a63c54:/workspaces/venv# cd ..
937f59a63c54:/workspaces# . venv/bin/activate
(venv) 937f59a63c54:/workspaces# python
Python 3.9.6 (default, Jul 21 2021, 02:01:12) 
[GCC 10.3.1 20210424] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import ulid
>>> id1 = ulid.new()
>>> id1
<ULID('01FB4827H7K34PAZ2209M6WBCE')>
>>> id1.str
'01FB4827H7K34PAZ2209M6WBCE'

Python環境つくったのは、2019年末以来かな

4
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
4
0