npx skills add https://github.com/baggiponte/skills --skill build-python-dockerfiles使用此技能通过 uv 和多阶段构建为 Python 项目编写 Dockerfile。默认模式基于 Hynek Schlawack 的文章 使用 uv 构建生产就绪的 Python Docker 容器。
python:<version>-slim。ubuntu:noble 这样的操作系统镜像。uv sync 步骤中。build 和 final。Use this skill to author Dockerfiles for Python projects using uv and multi-stage builds. The default pattern is based on Hynek Schlawack's article Production-ready Python Docker Containers with uv.
python:<version>-slim for generic Python services.ubuntu:noble when system packages or org-standard base images matter.uv sync steps.广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
RUN uv sync 步骤中。uv 缓存使用 BuildKit 缓存挂载。UV_PROJECT_ENVIRONMENT=/app 并将 /app 复制到运行时镜像中。UV_COMPILE_BYTECODE=1 字节编译 Python 文件以加快启动速度。uv sync --locked。ghcr.io/astral-sh/uv:<version> 复制 uv。UV_LINK_MODE=copyUV_COMPILE_BYTECODE=1UV_PYTHON_DOWNLOADS=neverUV_PROJECT_ENVIRONMENT=/appuv 无法清晰推断出预期的解释器时,设置 UV_PYTHON。uv 缓存使用 BuildKit 缓存挂载,通常是 /root/.cache/uv 或 /root/.cache。ENV 将密钥硬编码到 Dockerfile 中。将此作为打包应用程序的默认起点:
# syntax=docker/dockerfile:1.9
FROM python:3.13-slim AS build
COPY --from=ghcr.io/astral-sh/uv:0.8.15 /uv /uvx /bin/
ENV UV_LINK_MODE=copy \
UV_COMPILE_BYTECODE=1 \
UV_PYTHON_DOWNLOADS=never \
UV_PROJECT_ENVIRONMENT=/app
WORKDIR /src
COPY pyproject.toml uv.lock ./
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --locked --no-dev --no-install-project
COPY . /src
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --locked --no-dev --no-editable
FROM python:3.13-slim AS final
ENV PATH="/app/bin:$PATH"
STOPSIGNAL SIGINT
RUN groupadd --system app && useradd --system --gid app --home-dir /app app
COPY --from=build --chown=app:app /app /app
USER app
WORKDIR /app
CMD ["python", "-m", "your_package"]
请记住以下适配规则:
uv sync 步骤,并在复制 /app 后将源代码复制到运行时镜像中。ENTRYPOINT 脚本并保留 STOPSIGNAL SIGINT。python:<version>-slim 时,请显式安装确切的运行时 Python 包,并在需要时设置 UV_PYTHON。选择与项目类型匹配的启动命令。
EXPOSE 8000
CMD ["uvicorn", "your_package.main:app", "--host", "0.0.0.0", "--port", "8000"]
EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "your_project.wsgi:application"]
EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "your_package.wsgi:app"]
CMD ["python", "-m", "your_package.worker"]
ENTRYPOINT ["python", "-m", "your_package.cli"]
对 CMD 和 ENTRYPOINT 使用 JSON 数组语法。
在最终确定输出之前运行此清单:
uv 是从官方镜像复制的。uv 使用了 BuildKit 缓存挂载。UV_PROJECT_ENVIRONMENT=/app 并将 /app 复制到运行时镜像中。PATH 包含 /app/bin。STOPSIGNAL SIGINT。EXPOSE。.dockerignore。为用户生成 Dockerfile 时:
.dockerignore 建议。每周安装次数
1
仓库
首次出现
1 天前
安全审计
安装于
zencoder1
amp1
cline1
openclaw1
opencode1
cursor1
buildfinalRUN uv sync steps.uv cache.UV_PROJECT_ENVIRONMENT=/app and copy /app into runtime image.UV_COMPILE_BYTECODE=1.uv sync --locked for deployment builds.uv from ghcr.io/astral-sh/uv:<version>.UV_LINK_MODE=copyUV_COMPILE_BYTECODE=1UV_PYTHON_DOWNLOADS=neverUV_PROJECT_ENVIRONMENT=/appUV_PYTHON when the base image has multiple Python interpreters or when uv cannot infer the intended one cleanly.uv cache, typically /root/.cache/uv or /root/.cache.ENV.Use this as the default starting point for packaged applications:
# syntax=docker/dockerfile:1.9
FROM python:3.13-slim AS build
COPY --from=ghcr.io/astral-sh/uv:0.8.15 /uv /uvx /bin/
ENV UV_LINK_MODE=copy \
UV_COMPILE_BYTECODE=1 \
UV_PYTHON_DOWNLOADS=never \
UV_PROJECT_ENVIRONMENT=/app
WORKDIR /src
COPY pyproject.toml uv.lock ./
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --locked --no-dev --no-install-project
COPY . /src
RUN --mount=type=cache,target=/root/.cache/uv \
uv sync --locked --no-dev --no-editable
FROM python:3.13-slim AS final
ENV PATH="/app/bin:$PATH"
STOPSIGNAL SIGINT
RUN groupadd --system app && useradd --system --gid app --home-dir /app app
COPY --from=build --chown=app:app /app /app
USER app
WORKDIR /app
CMD ["python", "-m", "your_package"]
Keep the following adaptation rules in mind:
uv sync step and copy the source into the runtime image after copying /app.ENTRYPOINT script and keep STOPSIGNAL SIGINT.python:<version>-slim, install the exact runtime Python packages explicitly and set UV_PYTHON if needed.Pick the startup command that matches the project type.
EXPOSE 8000
CMD ["uvicorn", "your_package.main:app", "--host", "0.0.0.0", "--port", "8000"]
EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "your_project.wsgi:application"]
EXPOSE 8000
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "your_package.wsgi:app"]
CMD ["python", "-m", "your_package.worker"]
ENTRYPOINT ["python", "-m", "your_package.cli"]
Use JSON-array syntax for CMD and ENTRYPOINT.
Run this checklist before finalizing output:
uv is copied from the official image.uv.UV_PROJECT_ENVIRONMENT=/app is set and /app is copied into the runtime image.PATH includes /app/bin when executables live in the project environment.STOPSIGNAL SIGINT.EXPOSE for network services..dockerignore that excludes VCS data, caches, build artifacts, and local virtualenvs.When generating a Dockerfile for a user:
.dockerignore suggestion when missing.Weekly Installs
1
Repository
First Seen
1 day ago
Security Audits
Installed on
zencoder1
amp1
cline1
openclaw1
opencode1
cursor1
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
147,400 周安装
Python Asyncio异步编程指南:构建与审查生产级异步代码
1 周安装
数据叙事与可视化技能:遵循《Storytelling with Data》6大原则,优化图表与演示
1 周安装
网页动画设计与优化指南 | 遵循《Animation at Work》原则,提升用户体验与性能
1 周安装
GitHub项目看板配置与CLI命令指南 - Simple History项目管理
1 周安装
Atlassian REST API 技能:Jira 与 Confluence 自动化操作指南
1 周安装
Bitget CLI 工具:一站式加密货币交易命令行接口,支持现货合约杠杆跟单
1 周安装