Working multi-stage container build

This commit is contained in:
Imbus 2023-10-18 03:47:29 +02:00
parent 2b1e350ce1
commit 107bb05d47
3 changed files with 30 additions and 8 deletions

View file

@ -1,7 +1,26 @@
# The rust:latest-alpine uses musl libc
FROM docker.io/rust:latest as builder
workdir /app
ADD . /app
RUN cargo clean
RUN cargo build --release
WORKDIR /build
ADD . /build
RUN apt update
RUN apt install musl musl-dev musl-tools -y
RUN rustup target add x86_64-unknown-linux-musl
RUN cargo build --target x86_64-unknown-linux-musl --release
FROM docker.io/alpine:latest as runner
# Add a non-root user for running the server
RUN addgroup -S user && adduser -S user -G user
# Copy the server binary and the public directory
WORKDIR /runner
COPY --from=builder /build/target/x86_64-unknown-linux-musl/release/server /runner/server
COPY --from=builder /build/public /runner/public
# Make sure the user can access the files
RUN chown -R user:user /runner
USER user
# Run the server
EXPOSE 8080
CMD ["cargo", "run", "--release"]
CMD ["./server"]