Skip to content

Commit 0f5c3cc

Browse files
committed
feat(docker): Rewrite dockerfile
1 parent cc42534 commit 0f5c3cc

File tree

1 file changed

+55
-71
lines changed

1 file changed

+55
-71
lines changed

Dockerfile

Lines changed: 55 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,88 +5,72 @@ ARG MUSL_TARGET="x86_64-linux-musl"
55
# The crate features to build this with
66
ARG FEATURES=""
77

8-
FROM --platform=$BUILDPLATFORM docker.io/alpine:latest as build
8+
FROM --platform=$BUILDPLATFORM rust:latest AS chef
99
ARG RUST_TARGET
1010
ARG MUSL_TARGET
1111
ARG FEATURES
1212

13-
RUN apk upgrade && \
14-
apk add curl gcc musl-dev && \
15-
curl -sSf https://sh.rustup.rs | sh -s -- --profile minimal --default-toolchain nightly --component rust-src -y
16-
17-
RUN source $HOME/.cargo/env && \
18-
mkdir -p /app/.cargo && \
19-
if [ "$RUST_TARGET" != $(rustup target list --installed) ]; then \
20-
rustup target add $RUST_TARGET && \
21-
curl -L "https://musl.cc/$MUSL_TARGET-cross.tgz" -o /toolchain.tgz && \
22-
tar xf toolchain.tgz && \
23-
ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-gcc" "/usr/bin/$MUSL_TARGET-gcc" && \
24-
ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-ld" "/usr/bin/$MUSL_TARGET-ld" && \
25-
ln -s "/$MUSL_TARGET-cross/bin/$MUSL_TARGET-strip" "/usr/bin/actual-strip" && \
26-
GCC_VERSION=$($MUSL_TARGET-gcc --version | grep gcc | awk '{print $3}') && \
27-
echo -e "\
28-
[build]\n\
29-
rustflags = [\"-L\", \"native=/$MUSL_TARGET-cross/$MUSL_TARGET/lib\", \"-L\", \"native=/$MUSL_TARGET-cross/lib/gcc/$MUSL_TARGET/$GCC_VERSION/\", \"-l\", \"static=gcc\", \"-Z\", \"gcc-ld=lld\"]\n\
30-
[target.$RUST_TARGET]\n\
31-
linker = \"$MUSL_TARGET-gcc\"\n\
32-
[unstable]\n\
33-
build-std = [\"std\", \"panic_abort\"]\n\
34-
" > /app/.cargo/config; \
35-
else \
36-
echo "skipping toolchain as we are native" && \
37-
echo -e "\
38-
[build]\n\
39-
rustflags = [\"-L\", \"native=/usr/lib\"]\n\
40-
[unstable]\n\
41-
build-std = [\"std\", \"panic_abort\"]\n\
42-
" > /app/.cargo/config && \
43-
ln -s /usr/bin/strip /usr/bin/actual-strip; \
44-
fi
13+
RUN <<EOT
14+
set -ex
15+
apt-get update
16+
apt-get install --assume-yes musl-dev clang lld
17+
EOT
18+
19+
RUN rustup target add $RUST_TARGET
20+
21+
RUN cargo install cargo-chef --locked
22+
23+
COPY <<EOF /app/.cargo/config
24+
[env]
25+
CC_aarch64-unknown-linux-musl = "clang -target aarch64-unknown-linux-musl -fuse-ld=lld"
26+
CXX_aarch64-unknown-linux-musl = "clang++ -target aarch64-unknown-linux-musl -fuse-ld=lld"
27+
CC_x86_64-unknown-linux-musl = "clang -target x86_64-unknown-linux-musl -fuse-ld=lld"
28+
CXX_x86_64-unknown-linux-musl = "clang++ -target x86_64-unknown-linux-musl -fuse-ld=lld"
29+
30+
[target.aarch64-unknown-linux-musl]
31+
linker = "clang"
32+
rustflags = ["-C", "link-args=-target aarch64-unknown-linux-musl -fuse-ld=lld"]
33+
34+
[target.x86_64-unknown-linux-musl]
35+
linker = "clang"
36+
rustflags = ["-C", "link-args=-target x86_64-unknown-linux-musl -fuse-ld=lld"]
37+
38+
#[unstable]
39+
#build-std = ["std", "panic_abort"]
40+
EOF
4541

4642
WORKDIR /app
4743

48-
COPY ./Cargo.lock ./Cargo.lock
49-
COPY ./Cargo.toml ./Cargo.toml
50-
51-
# We need a source directory so that it builds the dependencies and an empty
52-
# binary.
53-
RUN mkdir src/
54-
RUN echo 'fn main() {}' > ./src/main.rs
55-
RUN source $HOME/.cargo/env && \
56-
if [ "$FEATURES" == "" ]; then \
57-
cargo build --release \
58-
--target="$RUST_TARGET"; \
59-
else \
60-
cargo build --release \
61-
--target="$RUST_TARGET" --features="$FEATURES"; \
44+
FROM chef AS planner
45+
COPY . .
46+
RUN cargo chef prepare --recipe-path recipe.json
47+
48+
FROM chef AS builder
49+
COPY --from=planner /app/recipe.json recipe.json
50+
RUN <<EOF bash
51+
set -ex
52+
if test "$FEATURES" = ""; then
53+
cargo chef cook --target "$RUST_TARGET" --release --recipe-path recipe.json
54+
else
55+
cargo chef cook --target "$RUST_TARGET" --features="$FEATURES" --release --recipe-path recipe.json
56+
fi
57+
EOF
58+
59+
COPY . .
60+
61+
RUN <<EOF bash
62+
set -ex
63+
if test "$FEATURES" = "" ; then
64+
cargo build --release --target $RUST_TARGET
65+
else
66+
cargo build --release --target $RUST_TARGET --features="$FEATURES"
6267
fi
68+
cp target/$RUST_TARGET/release/twilight-http-proxy /twilight-http-proxy
69+
EOF
6370

64-
# Now, delete the fake source and copy in the actual source. This allows us to
65-
# have a previous compilation step for compiling the dependencies, while being
66-
# able to only copy in and compile the binary itself when something in the
67-
# source changes.
68-
#
69-
# This is very important. If we just copy in the source after copying in the
70-
# Cargo.lock and Cargo.toml, then every time the source changes the dependencies
71-
# would have to be re-downloaded and re-compiled.
72-
#
73-
# Also, remove the artifacts of building the binaries.
74-
RUN rm -f target/$RUST_TARGET/release/deps/twilight_http_proxy*
75-
COPY ./src ./src
76-
77-
RUN source $HOME/.cargo/env && \
78-
if [ "$FEATURES" == "" ]; then \
79-
cargo build --release \
80-
--target="$RUST_TARGET"; \
81-
else \
82-
cargo build --release \
83-
--target="$RUST_TARGET" --features="$FEATURES"; \
84-
fi && \
85-
cp target/$RUST_TARGET/release/twilight-http-proxy /twilight-http-proxy && \
86-
actual-strip /twilight-http-proxy
8771

8872
FROM scratch
8973

90-
COPY --from=build /twilight-http-proxy /twilight-http-proxy
74+
COPY --from=builder /twilight-http-proxy /twilight-http-proxy
9175

9276
CMD ["./twilight-http-proxy"]

0 commit comments

Comments
 (0)