diff --git a/blueprint/Dockerfile.liquid b/blueprint/Dockerfile.liquid new file mode 100644 index 00000000..95df1ada --- /dev/null +++ b/blueprint/Dockerfile.liquid @@ -0,0 +1,40 @@ +# `cargo-chef` is a cargo-subcommand that provides +# enhanced Docker layer caching for Rust projects. +FROM lukemathwalker/cargo-chef:latest AS chef +WORKDIR /app +# Force `rustup` to sync the toolchain in the base `chef` layer +# so that it doesn't happen more than once +COPY rust-toolchain.toml . +RUN rustup toolchain install + +FROM chef AS planner +COPY . . +# Compute a lock-like file for our project +RUN cargo chef prepare --recipe-path recipe.json + +FROM chef AS builder +COPY --from=planner /app/recipe.json recipe.json +# Build our project's dependencies, not our application! +# this is the caching Docker layer. +RUN cargo chef cook --release --recipe-path recipe.json +COPY . . +# Build our project +ENV SQLX_OFFLINE=true +RUN cargo build --release --bin {{project-name}}-web + +FROM debian:bookworm-slim AS runtime + +COPY --from=builder /app/target/release/{{project-name}}-web {{project-name}}-web + +# Enable backtraces to simplify debugging +# production panics. +ENV RUST_BACKTRACE=1 +# We don't want `anyhow` to capture backtraces for +# "routine" errors. Just panics. +ENV RUST_LIB_BACKTRACE=0 + +ENV APP_ENVIRONMENT=production +ENV APP_SERVER__PORT=3000 +ENV APP_SERVER__IP="0.0.0.0" +ENTRYPOINT ["./{{project-name}}-web"] +EXPOSE 3000 diff --git a/blueprint/README.md.liquid b/blueprint/README.md.liquid index 2ce87eee..504eceb1 100644 --- a/blueprint/README.md.liquid +++ b/blueprint/README.md.liquid @@ -75,3 +75,20 @@ Build the project's documentation with: ``` cargo doc --workspace --all-features ``` + +## Building the project's starter Docker image + +The generated project includes a production-ready Dockerfile + +In order to build it, follow these steps: + +``` +docker compose up +cargo db migrate +cargo db prepare +docker build . --tag {{project-name}} +``` + +The db steps are needed to generate query metadata that sqlx will use during compilation. + +Note that the first build can take a few minutes, but subsequent ones should be fast thanks to docker layer caching. diff --git a/blueprint/rust-toolchain.toml b/blueprint/rust-toolchain.toml new file mode 100644 index 00000000..b50665c2 --- /dev/null +++ b/blueprint/rust-toolchain.toml @@ -0,0 +1,4 @@ +# If you're making changes here. +# Change the Dockerfile to also use a matching version. +[toolchain] +channel = "1.85"