1+ # syntax=docker/dockerfile:1
2+
3+ # Comments are provided throughout this file to help you get started.
4+ # If you need more help, visit the Dockerfile reference guide at
5+ # https://docs.docker.com/engine/reference/builder/
6+
7+ # ###############################################################################
8+ # Create a stage for building the application.
9+
10+ ARG RUST_VERSION=1.70.0
11+ ARG APP_NAME=react-rust-postgres
12+ FROM rust:${RUST_VERSION}-slim-bullseye AS build
13+ ARG APP_NAME
14+ WORKDIR /app
15+
16+ # Build the application.
17+ # Leverage a cache mount to /usr/local/cargo/registry/
18+ # for downloaded dependencies and a cache mount to /app/target/ for
19+ # compiled dependencies which will speed up subsequent builds.
20+ # Leverage a bind mount to the src directory to avoid having to copy the
21+ # source code into the container. Once built, copy the executable to an
22+ # output directory before the cache mounted /app/target is unmounted.
23+ RUN --mount=type=bind,source=src,target=src \
24+ --mount=type=bind,source=Cargo.toml,target=Cargo.toml \
25+ --mount=type=bind,source=Cargo.lock,target=Cargo.lock \
26+ --mount=type=cache,target=/app/target/ \
27+ --mount=type=cache,target=/usr/local/cargo/registry/ \
28+ --mount=type=bind,source=migrations,target=migrations \
29+ <<EOF
30+ set -e
31+ cargo build --locked --release
32+ cp ./target/release/$APP_NAME /bin/server
33+ EOF
34+
35+ # ###############################################################################
36+ # Create a new stage for running the application that contains the minimal
37+ # runtime dependencies for the application. This often uses a different base
38+ # image from the build stage where the necessary files are copied from the build
39+ # stage.
40+ #
41+ # The example below uses the debian bullseye image as the foundation for running the app.
42+ # By specifying the "bullseye-slim" tag, it will also use whatever happens to be the
43+ # most recent version of that tag when you build your Dockerfile. If
44+ # reproducability is important, consider using a digest
45+ # (e.g., debian@sha256:ac707220fbd7b67fc19b112cee8170b41a9e97f703f588b2cdbbcdcecdd8af57).
46+ FROM debian:bullseye-slim AS final
47+
48+ # Create a non-privileged user that the app will run under.
49+ # See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/ #user
50+ ARG UID=10001
51+ RUN adduser \
52+ --disabled-password \
53+ --gecos "" \
54+ --home "/nonexistent" \
55+ --shell "/sbin/nologin" \
56+ --no-create-home \
57+ --uid "${UID}" \
58+ appuser
59+ USER appuser
60+
61+ # Copy the executable from the "build" stage.
62+ COPY --from=build /bin/server /bin/
63+
64+ # Expose the port that the application listens on.
65+ EXPOSE 8000
66+
67+ # What the container should run when it is started.
68+ CMD ["/bin/server" ]
0 commit comments