Commit a8f2bc3
authored
Use multi-stage build in Dockerfile (#142)
This resolves the conversation at #117 (comment)
Currently we install the build dependencies like g++ (`RUN apk add...`) in a separate layer. We try and purge them in a later step but this doesn't achieve anything because docker caches everything on a layer by layer basis, i.e. the previous layer will still contain them and hang around.
This PR fixes it by using a multi-stage build which reduces the image size by ~90%:
```
$ docker image ls
REPOSITORY TAG IMAGE ID CREATED SIZE
lnt-new-dockerfile latest 22cfaa9c28b0 14 minutes ago 88MB
lnt-old-dockerfile <none> 76a17f3f65ed 5 seconds ago 797MB
```
This patch also only copies over the minimal source files required so as to make sure the dependencies layer is invalidated as infrequently as possible. E.g. here is an example of building the image after touching a regular python source file. We no longer need to rebuild or redownload the dependencies, that layer is cached:
```
$ docker build -f docker/lnt.dockerfile .
[+] Building 22.0s (15/15) FINISHED docker:default
=> [internal] load build definition from lnt.dockerfile 0.0s
=> => transferring dockerfile: 1.73kB 0.0s
=> [internal] load metadata for docker.io/library/python:3.10-alpine 0.4s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 2B 0.0s
=> [internal] load build context 0.5s
=> => transferring context: 562.77kB 0.4s
=> CACHED [builder 1/7] FROM docker.io/library/python:3.10-alpine@sha256:b4da816c29d5d3067a979e299ea3e4856476a2b 0.0s
=> [final 2/4] RUN apk update && apk add --no-cache libpq 2.8s
=> CACHED [builder 2/7] RUN apk update && apk add --no-cache g++ postgresql-dev yaml-dev git libpq 0.0s
=> CACHED [builder 3/7] COPY pyproject.toml . 0.0s
=> CACHED [builder 4/7] COPY lnt/testing/profile lnt/testing/profile 0.0s
=> CACHED [builder 5/7] RUN pip install --user ".[server]" 0.0s
=> [builder 6/7] COPY . . 2.1s
=> [builder 7/7] RUN pip install --user . 17.0s
=> [final 3/4] COPY --from=builder /root/.local /root/.local 0.9s
=> [final 4/4] COPY docker/docker-entrypoint.sh docker/docker-entrypoint-log.sh docker/lnt-wait-db /usr/local/bi 0.0s
=> exporting to image 0.6s
=> => exporting layers 0.6s
=> => writing image sha256:527e0140763072f26d84ef42b0bb9dda9857625acb3474ac5f159960c8d20bc4 0.0s
```
We need to use a mock version for setuptools_scm, since it looks for a .git folder and we want to avoid copying that. That would cause the build dependency layers to be invalidated on every commit.1 parent 070f5f9 commit a8f2bc3
1 file changed
+22
-11
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
29 | | - | |
| 29 | + | |
30 | 30 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
35 | 39 | | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
42 | 52 | | |
43 | 53 | | |
44 | 54 | | |
45 | 55 | | |
46 | 56 | | |
47 | 57 | | |
| 58 | + | |
48 | 59 | | |
49 | 60 | | |
0 commit comments