容器化 Python 应用程序
先决条件
- 您已安装最新版本的 Docker Desktop。
- 您拥有一个 git 客户端。本节中的示例使用基于命令行的 git 客户端,但您可以使用任何客户端。
概述
本节将引导您完成容器化和运行 Python 应用程序的过程。
获取示例应用程序
示例应用程序使用流行的 FastAPI 框架。
克隆示例应用程序以与本指南一起使用。打开终端,将目录更改到您要工作的目录,并运行以下命令克隆存储库
$ git clone https://github.com/estebanx64/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
的文件,其中包含以下内容。
# 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
的文件,其中包含以下内容。
# 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
的文件,其中包含以下内容。
# 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
的文件,其中包含以下内容。
# 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
的文件,其中包含以下内容。
# 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
打开浏览器并在 https://127.0.0.1:8000 上查看应用程序。您应该看到一个简单的 FastAPI 应用程序。
在终端中,按 ctrl
+c
停止应用程序。
在后台运行应用程序
您可以通过添加 -d
选项来使应用程序脱离终端运行。在 python-docker-example
目录中,在终端中运行以下命令。
$ docker compose up --build -d
打开浏览器并在 https://127.0.0.1:8000 上查看应用程序。
要查看 OpenAPI 文档,您可以访问 https://127.0.0.1:8000/docs。
您应该看到一个简单的 FastAPI 应用程序。
在终端中,运行以下命令停止应用程序。
$ docker compose down
有关 Compose 命令的更多信息,请参见 Compose CLI 参考。
总结
在本节中,您学习了如何使用 Docker 容器化和运行您的 Python 应用程序。
相关信息
下一步
在下一节中,您将学习如何使用容器开发您的应用程序。