You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: python/the-basics/multi-stage-builds.html.md
+35-24Lines changed: 35 additions & 24 deletions
Original file line number
Diff line number
Diff line change
@@ -12,47 +12,58 @@ In short, we split the process of building and compiling dependencies and runnin
12
12
- The resulting image is smaller in size
13
13
- The 'attack surface' of your application is smaller
14
14
15
-
Let's make a multi-stage `Dockerfile` from scratch. Here's part 1:
16
-
17
-
<divclass="note icon">
18
-
In this example we assume the use of `poetry`, however you can adapt the file to work with other dependency managers too.
19
-
</div>
15
+
Let's make a multi-stage `Dockerfile` using uv, based on the [uv-docker-example](https://github.com/astral-sh/uv-docker-example/raw/refs/heads/main/multistage.Dockerfile). Here's the complete Dockerfile:
20
16
21
17
```dockerfile
22
-
FROM python:3.11.9-bookworm AS builder
18
+
# Builder stage
19
+
FROM ghcr.io/astral-sh/uv:python3.12-bookworm-slim AS builder
23
20
24
-
ENV PYTHONUNBUFFERED=1 \
25
-
PYTHONDONTWRITEBYTECODE=1
26
-
27
-
RUN pip install poetry && poetry config virtualenvs.in-project true
So what's going on here? First, we use a "fat" python 3.11.9 image and installing and building all dependencies. Defining it as `builder` gives us a way to interact with it later. What essentially happens here is exactly what happens when you install a project locally using poetry: a `.venv/` directory is created and in it are all your built dependencies and binaries. You can inspect your own `.venv/` folder to see what that looks like. This directory is the primary artifact that we want.
34
+
# Install the project
35
+
RUN --mount=type=cache,target=/root/.cache/uv \
36
+
uv sync --frozen --no-dev
37
37
38
-
Part 2, the runtime, looks something like this:
38
+
# Runtime stage
39
+
FROM python:3.12-slim-bookworm
39
40
40
-
```dockerfile
41
-
FROM python:3.11.9-slim-bookworm
41
+
# Copy the application from the builder
42
+
COPY --from=builder --chown=app:app /app /app
42
43
43
-
WORKDIR /app
44
-
45
-
COPY --from=builder /app .
46
-
COPY [python-app]/ ./[python-app]
44
+
# Place executables in the environment at the front of the path
Here we see very little actually going on; instead of the "fat" image, we now pick the slim variant. This one is about 5 times smaller in size, but is unable to compile many of the dependencies we would want compiled. We have already done that part though, so we can copy that `.venv/` folder over to this image without having to compile it again.
51
+
Let's break down what's happening in this Dockerfile:
52
+
53
+
1.**Builder Stage**:
54
+
- We start with the `ghcr.io/astral-sh/uv:python3.12-bookworm-slim` image, which includes uv.
55
+
- We set environment variables to compile bytecode and use copy mode for linking.
56
+
- We use `uv sync` to install dependencies, utilizing caching for faster builds.
57
+
- We copy the application code and install the project.
58
+
59
+
2.**Runtime Stage**:
60
+
- We use a slim Python image that matches the builder's Python version.
61
+
- We copy the application from the builder stage.
62
+
- We set the `PATH` to include the virtual environment's `bin` directory.
63
+
- Finally, we set the command to run our application.
52
64
53
65
With this setup our image will be around 200MB most of the time (depending on what else you include). This setup is used for nearly all Python apps you deploy on Fly.io.
54
66
55
67
<divclass="note icon">
56
68
The image size is largely dependent on what files you add in the dockerfile; by default the entire working directory is copied in. If you do not want to add certain files, you can specify them in a `.dockerignore` file.
0 commit comments