{"name":"python-dockerfile","title":"Python Dockerfile with uv","description":"A multi-stage Python Dockerfile that installs dependencies with uv from a lockfile, caches them between builds and runs the app as a non-root user.","category":"deploy","updated":"2026-11-30","url":"https://sniptide.com/templates/python-dockerfile","files":[{"name":"Dockerfile","language":"dockerfile","content":"# syntax=docker/dockerfile:1\n\n# Build stage: install dependencies into a virtual environment with uv.\nFROM python:3.13-slim AS build\n\nCOPY --from=ghcr.io/astral-sh/uv:latest /uv /usr/local/bin/uv\n\n# Compile bytecode for faster starts, and copy files out of the cache instead of linking them.\nENV UV_COMPILE_BYTECODE=1 \\\n    UV_LINK_MODE=copy \\\n    UV_PYTHON_DOWNLOADS=0\n\nWORKDIR /app\n\n# Dependencies first, so this layer stays cached until pyproject.toml or uv.lock changes.\nRUN --mount=type=cache,target=/root/.cache/uv \\\n    --mount=type=bind,source=uv.lock,target=uv.lock \\\n    --mount=type=bind,source=pyproject.toml,target=pyproject.toml \\\n    uv sync --locked --no-install-project --no-dev\n\n# Then the project itself.\nCOPY . .\nRUN --mount=type=cache,target=/root/.cache/uv \\\n    uv sync --locked --no-dev --no-editable\n\n# Runtime stage: the same Python, the virtual environment and the code, without uv.\nFROM python:3.13-slim\n\nRUN groupadd --system app && useradd --system --gid app --no-create-home app\n\nWORKDIR /app\nCOPY --from=build --chown=app:app /app /app\n\nENV PATH=\"/app/.venv/bin:$PATH\" \\\n    PYTHONUNBUFFERED=1\n\nUSER app\nEXPOSE 8000\n\nCMD [\"uvicorn\", \"app.main:app\", \"--host\", \"0.0.0.0\", \"--port\", \"8000\"]\n"},{"name":".dockerignore","language":"text","content":".git\n.venv\n__pycache__\n*.pyc\n.pytest_cache\n.ruff_cache\n.mypy_cache\n.env\n.env.*\n!.env.example\nDockerfile\n.dockerignore\ncompose.yaml\ntests\n"}]}