Skip to content

Commit 3584d1f

Browse files
committed
Use multi-stage builds
1 parent 584bb92 commit 3584d1f

File tree

2 files changed

+19
-34
lines changed

2 files changed

+19
-34
lines changed

Dockerfile

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
FROM alpine:latest
2-
COPY ./ /app
1+
# Start with a rust alpine image
2+
FROM rust:1-alpine3.10
3+
# if needed, install dependencies here
4+
#RUN apk add libseccomp-dev
5+
# set the workdir and copy the source into it
36
WORKDIR /app
4-
RUN apk add --no-cache libgcc \
5-
&& apk add --no-cache --virtual .build-rust rust cargo \
6-
&& cargo build --release \
7-
&& cp target/release/mini-docker-rust . \
8-
&& rm -rf target/ ~/.cargo/ \
9-
&& apk del --purge .build-rust
10-
ENTRYPOINT ["./mini-docker-rust"]
7+
COPY ./ /app
8+
# do a release build
9+
RUN cargo build --release
10+
11+
# use a plain alpine image, the alpine version needs to match the builder
12+
FROM alpine:3.10
13+
# if needed, install dependencies here
14+
#RUN apk add libseccomp
15+
# copy the binary into the final image
16+
COPY --from=0 /app/target/release/mini-docker-rust .
17+
# set the binary as entrypoint
18+
ENTRYPOINT ["/mini-docker-rust"]

README.md

+2-25
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,12 @@
55

66
Very small rust docker image.
77

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.
99

1010
## See for yourself
1111

1212
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`.
1313

1414
## Annotated docker file
1515

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

Comments
 (0)