55 lines
No EOL
2 KiB
Docker
55 lines
No EOL
2 KiB
Docker
# Make sure RUST_VERSION matches the Rust version
|
||
ARG RUST_VERSION=1.92
|
||
ARG APP_NAME=docker-rust-hello
|
||
|
||
################################################################################
|
||
# Create a stage for building the application.
|
||
################################################################################
|
||
|
||
FROM dhi.io/rust:${RUST_VERSION}-alpine3.22-dev AS build
|
||
ARG APP_NAME
|
||
WORKDIR /app
|
||
|
||
# Install host build dependencies.
|
||
RUN apk add --no-cache clang lld musl-dev git
|
||
|
||
# Build the application.
|
||
RUN --mount=type=bind,source=src,target=src \
|
||
--mount=type=bind,source=Cargo.toml,target=Cargo.toml \
|
||
--mount=type=bind,source=Cargo.lock,target=Cargo.lock \
|
||
--mount=type=cache,target=/app/target/ \
|
||
--mount=type=cache,target=/usr/local/cargo/git/db \
|
||
--mount=type=cache,target=/usr/local/cargo/registry/ \
|
||
cargo build --locked --release && \
|
||
cp ./target/release/$APP_NAME /bin/server
|
||
|
||
################################################################################
|
||
# Create a new stage for running the application that contains the minimal
|
||
# We use dhi.io/static for the final stage because itâs a minimal Docker Hardened Image runtime (basically âjust # enough OS to run the binaryâ), which helps keep the image small and with a lower attack surface compared to a # # full Alpine/Debian runtime.
|
||
################################################################################
|
||
|
||
FROM dhi.io/static:20250419 AS final
|
||
|
||
# Create a non-privileged user that the app will run under.
|
||
ARG UID=10001
|
||
RUN adduser \
|
||
--disabled-password \
|
||
--gecos "" \
|
||
--home "/nonexistent" \
|
||
--shell "/sbin/nologin" \
|
||
--no-create-home \
|
||
--uid "${UID}" \
|
||
appuser
|
||
USER appuser
|
||
|
||
# Copy the executable from the "build" stage.
|
||
COPY --from=build /bin/server /bin/
|
||
|
||
# Configure rocket to listen on all interfaces.
|
||
ENV ROCKET_ADDRESS=0.0.0.0
|
||
|
||
# Expose the port that the application listens on.
|
||
EXPOSE 8000
|
||
|
||
# What the container should run when it is started.
|
||
CMD ["/bin/server"] |