容器化 Python 应用

先决条件

  • 你已安装最新版本的 Docker Desktop
  • 你有一个 Git 客户端。本节中的示例使用基于命令行的 Git 客户端,但你可以使用任何客户端。

概览

本节将引导你完成容器化和运行 Python 应用的过程。

获取示例应用

示例应用使用了流行的 FastAPI 框架。

克隆示例应用以便与本指南一起使用。打开终端,将目录切换到你想要工作的目录,然后运行以下命令克隆仓库:

$ git clone https://github.com/estebanx64/python-docker-example && cd python-docker-example

初始化 Docker 资产

现在你已经有了应用,可以创建必要的 Docker 资产来容器化你的应用了。你可以使用 Docker Desktop 内置的 Docker Init 功能来简化此过程,也可以手动创建资产。


python-docker-example 目录中,运行 docker init 命令。docker init 提供了一些默认配置,但你需要回答关于应用的一些问题。例如,此应用使用 FastAPI 运行。参考以下示例回答 docker init 的提示,并对你的提示使用相同的答案。

$ docker init
Welcome to the Docker Init CLI!

This utility will walk you through creating the following files with sensible defaults for your project:
  - .dockerignore
  - Dockerfile
  - compose.yaml
  - README.Docker.md

Let's get started!

? What application platform does your project use? Python
? What version of Python do you want to use? 3.11.4
? What port do you want your app to listen on? 8000
? What is the command to run your app? python3 -m uvicorn app:app --host=0.0.0.0 --port=8000

创建一个名为 .gitignore 的文件,内容如下。

.gitignore
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

如果你没有安装 Docker Desktop 或更喜欢手动创建资产,可以在项目目录中创建以下文件。

创建一个名为 Dockerfile 的文件,内容如下。

Dockerfile
# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.net.cn/go/dockerfile-reference/

# Want to help us make this template better? Share your feedback here: https://forms.gle/ybq9Krt8jtBL3iCk7

ARG PYTHON_VERSION=3.11.4
FROM python:${PYTHON_VERSION}-slim AS base

# Prevents Python from writing pyc files.
ENV PYTHONDONTWRITEBYTECODE=1

# Keeps Python from buffering stdout and stderr to avoid situations where
# the application crashes without emitting any logs due to buffering.
ENV PYTHONUNBUFFERED=1

WORKDIR /app

# Create a non-privileged user that the app will run under.
# See https://docs.docker.net.cn/go/dockerfile-user-best-practices/
ARG UID=10001
RUN adduser \
    --disabled-password \
    --gecos "" \
    --home "/nonexistent" \
    --shell "/sbin/nologin" \
    --no-create-home \
    --uid "${UID}" \
    appuser

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds.
# Leverage a bind mount to requirements.txt to avoid having to copy them into
# into this layer.
RUN --mount=type=cache,target=/root/.cache/pip \
    --mount=type=bind,source=requirements.txt,target=requirements.txt \
    python -m pip install -r requirements.txt

# Switch to the non-privileged user to run the application.
USER appuser

# Copy the source code into the container.
COPY . .

# Expose the port that the application listens on.
EXPOSE 8000

# Run the application.
CMD python3 -m uvicorn app:app --host=0.0.0.0 --port=8000

创建一个名为 compose.yaml 的文件,内容如下。

compose.yaml
# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Docker Compose reference guide at
# https://docs.docker.net.cn/go/compose-spec-reference/

# Here the instructions define your application as a service called "server".
# This service is built from the Dockerfile in the current directory.
# You can add other services your application may depend on here, such as a
# database or a cache. For examples, see the Awesome Compose repository:
# https://github.com/docker/awesome-compose
services:
  server:
    build:
      context: .
    ports:
      - 8000:8000

创建一个名为 .dockerignore 的文件,内容如下。

.dockerignore
# Include any files or directories that you don't want to be copied to your
# container here (e.g., local build artifacts, temporary files, etc.).
#
# For more help, visit the .dockerignore file reference guide at
# https://docs.docker.net.cn/go/build-context-dockerignore/

**/.DS_Store
**/__pycache__
**/.venv
**/.classpath
**/.dockerignore
**/.env
**/.git
**/.gitignore
**/.project
**/.settings
**/.toolstarget
**/.vs
**/.vscode
**/*.*proj.user
**/*.dbmdl
**/*.jfm
**/bin
**/charts
**/docker-compose*
**/compose.y*ml
**/Dockerfile*
**/node_modules
**/npm-debug.log
**/obj
**/secrets.dev.yaml
**/values.dev.yaml
LICENSE
README.md

创建一个名为 .gitignore 的文件,内容如下。

.gitignore
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

现在你的 python-docker-example 目录应该包含以下内容。

├── python-docker-example/
│ ├── app.py
│ ├── requirements.txt
│ ├── .dockerignore
│ ├── .gitignore
│ ├── compose.yaml
│ ├── Dockerfile
│ └── README.md

要了解有关这些文件的更多信息,请参阅以下内容:

运行应用

python-docker-example 目录中,在终端中运行以下命令。

$ docker compose up --build

打开浏览器并在 http://localhost:8000 查看应用。你应该会看到一个简单的 FastAPI 应用。

在终端中,按 ctrl+c 停止应用。

在后台运行应用

通过添加 -d 选项,你可以在终端中以分离模式运行应用。在 python-docker-example 目录中,在终端中运行以下命令。

$ docker compose up --build -d

打开浏览器并在 http://localhost:8000

要查看 OpenAPI 文档,你可以访问 http://localhost:8000/docs

你应该会看到一个简单的 FastAPI 应用。

在终端中,运行以下命令停止应用。

$ docker compose down

有关 Compose 命令的更多信息,请参阅 Compose CLI 参考

总结

在本节中,你学习了如何使用 Docker 对你的 Python 应用进行容器化并运行它。

相关信息

下一步

在下一节中,你将学习如何使用容器开发你的应用。

页面选项