|
| 1 | +# mini-docker-rust |
| 2 | + |
| 3 | +Very small rust docker image. |
| 4 | + |
| 5 | +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 6.55MB during my tests. |
| 6 | + |
| 7 | +## See for yourself |
| 8 | + |
| 9 | +You don't need to install anything besides docker. Build with `docker build -t mini-docker-rust .` and run with `docker build -t mini-docker-rust`. |
| 10 | + |
| 11 | +## Annotated docker file |
| 12 | + |
| 13 | +``` |
| 14 | +# Start with alpine edge as a base image. (edge is required at the point of writing) |
| 15 | +FROM alpine:edge |
| 16 | +# Copy our project into the image (see .dockerignore for exclusions) |
| 17 | +COPY ./ /app |
| 18 | +# Switch the current directory to /app |
| 19 | +WORKDIR /app |
| 20 | +# This does multiple things in one go to keep the image size and layer number extremly low: |
| 21 | +# First we enable the testing repo (required at the point of writing) |
| 22 | +RUN echo http://nl.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories \ |
| 23 | + # llvm-libunwind is required to run the final rust binary, so we install it first |
| 24 | + && apk add --no-cache llvm-libunwind \ |
| 25 | + # Next, we install rust and cargo and tag them in a virtual package called `.build-rust` |
| 26 | + && apk add --no-cache --virtual .build-rust rust cargo \ |
| 27 | + # Finally, we build our project |
| 28 | + && cargo build --release \ |
| 29 | + # After that we copy our binary to the project root (you need to adjust this to your project) |
| 30 | + && cp target/release/foo . \ |
| 31 | + # And discard the target/ directory so it won't bloat our image |
| 32 | + && rm -rf target/ \ |
| 33 | + # As the final cleanup step we uninstall our virtual package |
| 34 | + # This uninstalls cargo, rust and all dependencies that aren't needed anymore so they won't end up in the final image |
| 35 | + && apk del --purge .build-rust |
| 36 | +# Finally, we configure our binary as entrypoint (you need to adjust this too) |
| 37 | +ENTRYPOINT ["./foo"] |
| 38 | +``` |
0 commit comments