|
5 | 5 |
|
6 | 6 | Very small rust docker image.
|
7 | 7 |
|
8 |
| -This is an example project on how to build very small docker images for a rust project. The resulting image for a working hello world was about 5.87MB during my tests. |
| 8 | +This is an example project on how to build very small docker images for a rust project. The resulting image for a working hello world was about 8.1MB during my tests. |
9 | 9 |
|
10 | 10 | ## See for yourself
|
11 | 11 |
|
12 | 12 | You don't need to install anything besides docker. Build with `docker build -t mini-docker-rust .` and run with `docker run mini-docker-rust`.
|
13 | 13 |
|
14 | 14 | ## Annotated docker file
|
15 | 15 |
|
16 |
| -``` |
17 |
| -# Start with alpine as base image |
18 |
| -FROM alpine:latest |
19 |
| -# Copy our project into the image (see .dockerignore for exclusions) |
20 |
| -COPY ./ /app |
21 |
| -# Switch the current directory to /app |
22 |
| -WORKDIR /app |
23 |
| -# This does multiple things in one go to keep the image size and layer number extremly low: |
24 |
| -# llvm-libunwind is required to run the final rust binary, so we install it first |
25 |
| -RUN apk add --no-cache llvm-libunwind \ |
26 |
| - # Next, we install rust and cargo and tag them in a virtual package called `.build-rust` |
27 |
| - && apk add --no-cache --virtual .build-rust rust cargo \ |
28 |
| - # Finally, we build our project |
29 |
| - && cargo build --release \ |
30 |
| - # After that we copy our binary to the project root (you need to adjust this to your project) |
31 |
| - && cp target/release/mini-docker-rust . \ |
32 |
| - # Discard the target/ and ~/.cargo/ directory so it won't bloat our image |
33 |
| - && rm -rf target/ ~/.cargo/ \ |
34 |
| - # As the final cleanup step we uninstall our virtual package |
35 |
| - # This uninstalls cargo, rust and all dependencies that aren't needed anymore so they won't end up in the final image |
36 |
| - && apk del --purge .build-rust |
37 |
| -# Finally, we configure our binary as entrypoint (you need to adjust this too) |
38 |
| -ENTRYPOINT ["./mini-docker-rust"] |
39 |
| -``` |
| 16 | +See [Dockerfile](Dockerfile). |
0 commit comments