Skip to content

Commit 9651e01

Browse files
committed
feat(docker): Optimize build for cross-compilation and caching deps
1 parent e329b9a commit 9651e01

2 files changed

Lines changed: 23 additions & 6 deletions

File tree

.dockerignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/target/
2-
Cargo.lock
32
.env
43
.git
5-
docker/
4+
docker/
5+
.devcontainer/
6+
.vscode/

docker/Dockerfile

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,29 @@ FROM rust:1.90-bookworm as Builder
22

33
ENV RUST_LOG=info,docker_proxy_filter=debug
44

5-
WORKDIR /usr/src/app
6-
COPY . .
5+
WORKDIR /app
76

7+
RUN cargo init
88

9-
RUN cargo install --target x86_64-unknown-linux-gnu --path .
9+
COPY Cargo.lock Cargo.toml ./
10+
11+
# build dummy project with dependencies to get them compiled and cached
12+
RUN cargo build --release --target $(rustc --print host-tuple)
13+
14+
# copy full app code
15+
COPY src/ src/
16+
# updated modified timestamp so build runs again
17+
RUN touch src/main.rs
18+
19+
# build full app to host arch and copy to generic location
20+
RUN export ARCH=$(rustc --print host-tuple); \
21+
cargo build --release --target $ARCH \
22+
&& mkdir build \
23+
&& cp target/$ARCH/release/docker-proxy-filter build/docker-proxy-filter
1024

1125
FROM debian:bookworm-slim
26+
# need ssl for web server
1227
RUN apt-get update && apt-get upgrade -y && apt-get install openssl -y && apt clean && rm -rf /var/lib/apt/lists/*
13-
COPY --from=builder /usr/src/app/target/x86_64-unknown-linux-gnu/release/docker-proxy-filter /usr/local/bin/docker-proxy-filter
28+
# copy built app from generic location
29+
COPY --from=builder /app/build/docker-proxy-filter /usr/local/bin/docker-proxy-filter
1430
CMD ["docker-proxy-filter"]

0 commit comments

Comments
 (0)