Back to DockerPython

See Alse Visual Studio Code -> RemoteCondaAlpine_LinuxFlask

Python Development in Docker

1. Python 官方镜像

1.1. python:<version>-alpine

$ docker images
REPOSITORY                                                      TAG                 IMAGE ID            CREATED             SIZE
python                                                          2                   3edf9092825f        13 days ago         884MB
python                                                          3.7-alpine          127f689add47        13 days ago         98.6MB
python                                                          2.7-alpine          df2b31967ad0        2 weeks ago         61.5MB
python                                                          3                   34a518642c76        6 weeks ago         929MB

1.2. Run a single Python script

对于许多简单的单文件项目,不必编写 Dockerfile,可以直接使用 Python Docker 映像运行 Python 脚本:

$ docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:3 python your-daemon-or-script.py

Python 2: $ docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp python:2 python your-daemon-or-script.py

1.3. 封装项目的 Dockerfile

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" ]

FROM python:2

WORKDIR /usr/src/app

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

COPY . .

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

$ docker build -t my-python-app .
$ docker run -it --rm --name my-running-app my-python-app

2. Practices

2.1. VS Code - Python

git clone https://github.com/Microsoft/vscode-remote-try-python

2.2. Flask

Docker + Flask - A Simple Tutorial

https://flask.palletsprojects.com/en/1.1.x/quickstart/#a-minimal-application

https://github.com/twotwo/docker-flask-dev

https://github.com/amouat/example_app/blob/master/Dockerfile

3. Reference