diff --git a/Dockerfile b/Dockerfile index 28d6728..f594de8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,17 +1,33 @@ -# syntax=docker/dockerfile:1.2 -FROM rustlang/rust:nightly AS builder +FROM rust:alpine AS builder + +# Switch to the nightly build +RUN rustup default nightly WORKDIR /usr/src/app -# Build the app -COPY . . -RUN --mount=type=cache,target=/usr/src/app/target cargo install --path . +# 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 + +# 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 . -FROM debian:buster-slim +# Now, we create the actual image +FROM alpine:latest # Install dependencies -RUN apt update && apt install -y openssl +RUN apk update && apk add --no-cache openssl COPY --from=builder /usr/local/cargo/bin/rust-api /usr/local/bin/rust-api