diff --git a/Dockerfile b/Dockerfile index a5148fe..9de43f3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,34 +1,28 @@ -FROM rust:alpine AS builder +FROM alpine:latest AS builder + +ENV PATH "$PATH:/root/.cargo/bin" # Switch to the nightly build -RUN rustup default nightly - WORKDIR /usr/src/app -# Install build dependencies & create a new dummy project -# that we use as a build cache -RUN apk update && \ - apk add --no-cache openssl openssl-dev musl-dev && \ - cargo init --bin +# Install build dependencies, install rustup & create dummy project +RUN apk update && apk add --no-cache openssl-dev build-base curl && \ + { curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --default-toolchain nightly; } && \ + rustup target add x86_64-unknown-linux-musl # Build the dependencies # This is done separately to reduce average compile time # Afterwards, we remove the dummy src directory -COPY Cargo.toml Cargo.lock ./ -RUN cargo build --release && \ - rm src/*.rs - # Now, we build our own source code -COPY src/ ./src -RUN cargo install --path . +COPY Cargo.toml Cargo.lock ./ +COPY src/ ./src/ +RUN cargo build --release --target x86_64-unknown-linux-musl # Now, we create the actual image -FROM alpine:latest +FROM scratch # Install dependencies -RUN apk update && apk add --no-cache openssl +COPY --from=builder /usr/src/app/target/x86_64-unknown-linux-musl/release/rust-api /rust-api -COPY --from=builder /usr/local/cargo/bin/rust-api /usr/local/bin/rust-api - -CMD ["rust-api"] +CMD ["/rust-api"]