Skip to content
Draft
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions blueprint/Dockerfile.liquid
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# `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

RUN adduser \
--disabled-password \
--gecos "" \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid 10001 \
"{{project-name}}"

USER {{project-name}}:{{project-name}}

COPY --from=builder --chown={{project-name}}:{{project-name}} /app/target/release/{{project-name}}-web bin

# 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"
Comment on lines +31 to +38
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These ENVs ideally aren't defined here.
APP_SERVER ones especiialy, they should be set by the config crate directly

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we actually have an open issue for that on restations too. Do you feel it's a must for this PR, or can it be done in a separate one?

ENTRYPOINT ["./bin"]
EXPOSE 3000
17 changes: 17 additions & 0 deletions blueprint/README.md.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -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.
4 changes: 4 additions & 0 deletions blueprint/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# If you're making changes here.
# Change the Dockerfile to also use a matching version.
[toolchain]
channel = "1.85"
Loading