|
| 1 | +# Dockerfile |
| 2 | + |
| 3 | +**Docker** is a popular choice for modern application deployment. However, creating a good Dockerfile from scratch can be challenging. This guide provides a **solid foundation** that works well for most Python projects. |
| 4 | + |
| 5 | +While the example below won't fit every use case, it offers an excellent starting point that you can adapt to your specific needs. |
| 6 | + |
| 7 | + |
| 8 | +## Quickstart |
| 9 | + |
| 10 | +For this example, we'll need to install [`docker`](https://docs.docker.com/get-docker/), |
| 11 | +[docker-compose](https://docs.docker.com/compose/install/) and |
| 12 | +[`uv`](https://docs.astral.sh/uv/getting-started/installation/). |
| 13 | + |
| 14 | +Then, let's create a new project with `uv`: |
| 15 | + |
| 16 | +```bash |
| 17 | +uv init app |
| 18 | +``` |
| 19 | + |
| 20 | +This will create a new project with a basic structure: |
| 21 | + |
| 22 | +```bash |
| 23 | +app/ |
| 24 | +├── main.py |
| 25 | +├── pyproject.toml |
| 26 | +└── README.md |
| 27 | +``` |
| 28 | + |
| 29 | +On `main.py`, let's create a simple ASGI application: |
| 30 | + |
| 31 | +```python title="main.py" |
| 32 | +async def app(scope, receive, send): |
| 33 | + body = "Hello, world!" |
| 34 | + await send( |
| 35 | + { |
| 36 | + "type": "http.response.start", |
| 37 | + "status": 200, |
| 38 | + "headers": [ |
| 39 | + [b"content-type", b"text/plain"], |
| 40 | + [b"content-length", len(body)], |
| 41 | + ], |
| 42 | + } |
| 43 | + ) |
| 44 | + await send( |
| 45 | + { |
| 46 | + "type": "http.response.body", |
| 47 | + "body": body.encode("utf-8"), |
| 48 | + } |
| 49 | + ) |
| 50 | +``` |
| 51 | + |
| 52 | +We need to include `uvicorn` in the dependencies: |
| 53 | + |
| 54 | +```bash |
| 55 | +uv add uvicorn |
| 56 | +``` |
| 57 | + |
| 58 | +This will also create a `uv.lock` file. :sunglasses: |
| 59 | + |
| 60 | +??? tip "What is `uv.lock`?" |
| 61 | + |
| 62 | + `uv.lock` is a `uv` specific lockfile. A lockfile is a file that contains the exact versions of the dependencies |
| 63 | + that were installed when the `uv.lock` file was created. |
| 64 | + |
| 65 | + This allows for deterministic builds and consistent deployments. |
| 66 | + |
| 67 | +Just to make sure everything is working, let's run the application: |
| 68 | + |
| 69 | +```bash |
| 70 | +uv run uvicorn main:app |
| 71 | +``` |
| 72 | + |
| 73 | +You should see the following output: |
| 74 | + |
| 75 | +```bash |
| 76 | +INFO: Started server process [62727] |
| 77 | +INFO: Waiting for application startup. |
| 78 | +INFO: ASGI 'lifespan' protocol appears unsupported. |
| 79 | +INFO: Application startup complete. |
| 80 | +INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
| 81 | +``` |
| 82 | + |
| 83 | +## Dockerfile |
| 84 | + |
| 85 | +We'll create a **cache-aware Dockerfile** that optimizes build times. The key strategy is to install dependencies first, then copy the project files. This approach leverages Docker's caching mechanism to significantly speed up rebuilds. |
| 86 | + |
| 87 | +```dockerfile title="Dockerfile" |
| 88 | +FROM python:3.12-slim |
| 89 | +COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ |
| 90 | + |
| 91 | +# Change the working directory to the `app` directory |
| 92 | +WORKDIR /app |
| 93 | + |
| 94 | +# Install dependencies |
| 95 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 96 | + --mount=type=bind,source=uv.lock,target=uv.lock \ |
| 97 | + --mount=type=bind,source=pyproject.toml,target=pyproject.toml \ |
| 98 | + uv sync --frozen --no-install-project |
| 99 | + |
| 100 | +# Copy the project into the image |
| 101 | +ADD . /app |
| 102 | + |
| 103 | +# Sync the project |
| 104 | +RUN --mount=type=cache,target=/root/.cache/uv \ |
| 105 | + uv sync --frozen |
| 106 | + |
| 107 | +# Run with uvicorn |
| 108 | +CMD ["uv", "run", "uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"] |
| 109 | +``` |
| 110 | + |
| 111 | +A common question is **"how many workers should I run?"**. The image above uses a single Uvicorn worker. |
| 112 | +The recommended approach is to let your orchestration system manage the number of deployed containers rather than |
| 113 | +relying on the process manager inside the container. |
| 114 | + |
| 115 | +You can read more about this in the |
| 116 | +[Decouple applications](https://docs.docker.com/build/building/best-practices/#decouple-applications) section |
| 117 | +of the Docker documentation. |
| 118 | + |
| 119 | +!!! warning "For production, create a non-root user!" |
| 120 | + When running in production, you should create a non-root user and run the container as that user. |
| 121 | + |
| 122 | +To make sure it works, let's build the image and run it: |
| 123 | + |
| 124 | +```bash |
| 125 | +docker build -t my-app . |
| 126 | +docker run -p 8000:8000 my-app |
| 127 | +``` |
| 128 | + |
| 129 | +For more information on using uv with Docker, refer to the |
| 130 | +[official uv Docker integration guide](https://docs.astral.sh/uv/guides/integration/docker/). |
| 131 | + |
| 132 | +## Docker Compose |
| 133 | + |
| 134 | +When running in development, it's often useful to have a way to hot-reload the application when code changes. |
| 135 | + |
| 136 | +Let's create a `docker-compose.yml` file to run the application: |
| 137 | + |
| 138 | +```yaml title="docker-compose.yml" |
| 139 | +services: |
| 140 | + backend: |
| 141 | + build: . |
| 142 | + ports: |
| 143 | + - "8000:8000" |
| 144 | + environment: |
| 145 | + - UVICORN_RELOAD=true |
| 146 | + volumes: |
| 147 | + - .:/app |
| 148 | + tty: true |
| 149 | +``` |
| 150 | +
|
| 151 | +You can run the application with `docker compose up` and it will automatically rebuild the image when code changes. |
| 152 | + |
| 153 | +Now you have a fully working development environment! :tada: |
0 commit comments