Skip to content

Commit 7679c54

Browse files
committed
Initial commit
0 parents  commit 7679c54

File tree

6 files changed

+67
-0
lines changed

6 files changed

+67
-0
lines changed

.dockerignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
target
2+
Dockerfile
3+
.dockerignore
4+
.git
5+
.gitignore

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
target/
2+
**/*.rs.bk
3+
*.swp
4+
*.swo

Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "mini-docker-rust"
3+
version = "0.1.0"
4+
authors = ["user"]
5+
6+
[dependencies]

Dockerfile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
FROM alpine:edge
2+
COPY ./ /app
3+
WORKDIR /app
4+
RUN echo http://nl.alpinelinux.org/alpine/edge/testing >> /etc/apk/repositories \
5+
&& apk add --no-cache llvm-libunwind \
6+
&& apk add --no-cache --virtual .build-rust rust cargo \
7+
&& cargo build --release \
8+
&& cp target/release/foo . \
9+
&& rm -rf target/ \
10+
&& apk del --purge .build-rust
11+
ENTRYPOINT ["./foo"]

README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
```

src/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("Hello, world!");
3+
}

0 commit comments

Comments
 (0)