0
0
Fork 0
mirror of https://git.verdigado.com/NB-Public/simple-wkd.git synced 2024-12-05 03:12:50 +01:00

Improve cross-compilation

This commit is contained in:
Delta1925 2023-05-20 18:18:41 +02:00
parent 8c41f87488
commit 218b635148
No known key found for this signature in database
GPG key ID: 1C21ACE44193CB25

View file

@ -1,32 +1,25 @@
ARG base="alpine:3.17" ARG base="alpine:3.18"
FROM ${base} AS bin-builder
# Disable static linking, see: https://users.rust-lang.org/t/sigsegv-with-program-linked-against-openssl-in-an-alpine-container/52172 FROM --platform=$BUILDPLATFORM tonistiigi/xx AS xx
FROM --platform=$BUILDPLATFORM ${base} AS bin-builder
ARG TARGETPLATFORM
COPY --from=xx / /
# Enable cargo sparse index for faster update times, see: https://blog.rust-lang.org/inside-rust/2023/01/30/cargo-sparse-protocol.html # Enable cargo sparse index for faster update times, see: https://blog.rust-lang.org/inside-rust/2023/01/30/cargo-sparse-protocol.html
ENV RUSTFLAGS='-C target-feature=-crt-static' \ ENV CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
CARGO_REGISTRIES_CRATES_IO_PROTOCOL=sparse
RUN apk add --no-cache cargo openssl-dev musl-dev RUN apk add clang lld musl-dev cargo
# This will build all dependencies and store them in docker's cache. COPY backend .
# This way, it won't be necessary to recompile everything everytime RUN xx-cargo build --release --target-dir ./build && \
COPY backend/Cargo.toml . xx-verify ./build/$(xx-cargo --print-target-triple)/release/simple-wkd
COPY backend/Cargo.lock . RUN mv ./build/$(xx-cargo --print-target-triple)/release/simple-wkd simple-wkd-executable
RUN echo '[[bin]]' >> Cargo.toml && \
echo 'name = "cache"' >> Cargo.toml && \
echo 'path = "cache.rs"' >> Cargo.toml && \
echo 'fn main() {eprintln!("Caching crates...")}' > cache.rs && \
cargo build --release
RUN rm cache.rs && \
rm Cargo.toml
# Build wimple-wkd
COPY backend/Cargo.toml .
COPY backend/src src
RUN cargo build --release
FROM --platform=$BUILDPLATFORM ${base} AS webpage-builder FROM --platform=$BUILDPLATFORM ${base} AS webpage-builder
RUN apk add --no-cache npm RUN apk add npm
COPY website . COPY website .
RUN npm install -g pnpm && \ RUN npm install -g pnpm && \
pnpm install && \ pnpm install && \
@ -39,13 +32,11 @@ RUN mv dist assets/webpage
FROM ${base} FROM ${base}
# The final image uses user `wkd` for added security # The final image uses user `wkd` for added security
# It also installs libgcc, because the executable is dynamically linked to it
WORKDIR /wkd WORKDIR /wkd
RUN apk add --no-cache libgcc && \ RUN adduser --no-create-home --disabled-password wkd && \
adduser --no-create-home --disabled-password wkd && \
chown -R wkd:wkd /wkd chown -R wkd:wkd /wkd
USER wkd USER wkd
COPY --from=webpage-builder assets assets COPY --from=webpage-builder assets assets
COPY --from=bin-builder target/release/simple-wkd wkd COPY --from=bin-builder simple-wkd-executable wkd
ENTRYPOINT [ "/wkd/wkd" ] ENTRYPOINT [ "/wkd/wkd" ]