Skip to content

Commit

Permalink
chore: Update Dockerfile with latest conventions. Update deps.
Browse files Browse the repository at this point in the history
Docker has recently gotten a new `docker init` command,
which creates an initial Dockerfile for you following best practices for
the specific language.

This updates the Dockerfile's used in this project to be closer to what
`docker init` would create for a rust project.

Also update rust/cargo deps.
  • Loading branch information
Christian Fosli committed Jul 9, 2023
1 parent 91d8af0 commit 809f788
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 52 deletions.
68 changes: 34 additions & 34 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 22 additions & 12 deletions front-end/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,30 @@ ARG VERSION=latest
ARG BUILD_PROFILE=release
ARG HIGHSCORE_API_BASE_URL=""
ENV HIGHSCORE_API_BASE_URL=$HIGHSCORE_API_BASE_URL
WORKDIR /usr/src
RUN curl --fail https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
COPY . .
WORKDIR /usr/src/front-end
# Build
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/usr/src/target \
wasm-pack build --${BUILD_PROFILE} --target web --out-name wasm-${VERSION} --out-dir ./out
# Copy over static files
RUN cp ./static/favicon.ico ./out/
RUN cp ./static/style.css ./out/style-${VERSION}.css
RUN sed 's/$VERSION/'"$VERSION"'/g' static/index.html > ./out/index.html
WORKDIR /app
# Build the app
# - Uses bind mounts so that it is not needed to copy the source code into the container
# (note that due to using a Cargo workspace the source files for all members members are needed)
# - Uses cache mounts to speed up subsequent builds
RUN --mount=type=bind,source=Cargo.toml,target=Cargo.toml \
--mount=type=bind,source=Cargo.lock,target=Cargo.lock \
--mount=type=bind,source=front-end/src,target=front-end/src \
--mount=type=bind,source=front-end/Cargo.toml,target=front-end/Cargo.toml \
--mount=type=bind,source=highscore-api/src,target=highscore-api/src \
--mount=type=bind,source=highscore-api/Cargo.toml,target=highscore-api/Cargo.toml \
--mount=type=bind,source=highscore-types/src,target=highscore-types/src \
--mount=type=bind,source=highscore-types/Cargo.toml,target=highscore-types/Cargo.toml \
--mount=type=cache,target=/app/target/ \
--mount=type=cache,target=/usr/local/cargo/registry/ \
cd front-end \
&& wasm-pack build --${BUILD_PROFILE} --target web --out-name wasm-${VERSION} --out-dir ./out
WORKDIR /app/front-end
# Copy over static files and add VERSION where useful
COPY front-end/static out/
RUN mv out/style.css out/style-${VERSION}.css && sed -i 's/$VERSION/'"$VERSION"'/g' out/index.html

FROM nginx AS final
COPY ./front-end/nginx.conf.template /etc/nginx/templates/default.conf.template
COPY --from=builder /usr/src/front-end/out /usr/share/nginx/html
COPY --from=builder /app/front-end/out /usr/share/nginx/html
EXPOSE 80
31 changes: 25 additions & 6 deletions highscore-api/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
FROM rust:1.70 AS builder
ARG CARGO_INSTALL_OPTIONS=''
WORKDIR /usr/src
COPY . .
WORKDIR /usr/src/highscore-api
RUN --mount=type=cache,target=/usr/local/cargo/registry \
--mount=type=cache,target=/usr/src/target \
cargo install ${CARGO_INSTALL_OPTIONS} --path .
WORKDIR /app
# Build the service
# - Uses bind mounts so that it is not needed to copy the source code into the container
# (note that due to using a Cargo workspace the source files for all members members are needed)
# - Uses cache mounts to speed up subsequent builds
RUN --mount=type=bind,source=Cargo.toml,target=Cargo.toml \
--mount=type=bind,source=Cargo.lock,target=Cargo.lock \
--mount=type=bind,source=front-end/src,target=front-end/src \
--mount=type=bind,source=front-end/Cargo.toml,target=front-end/Cargo.toml \
--mount=type=bind,source=highscore-api/src,target=highscore-api/src \
--mount=type=bind,source=highscore-api/Cargo.toml,target=highscore-api/Cargo.toml \
--mount=type=bind,source=highscore-types/src,target=highscore-types/src \
--mount=type=bind,source=highscore-types/Cargo.toml,target=highscore-types/Cargo.toml \
--mount=type=cache,target=/app/target/ \
--mount=type=cache,target=/usr/local/cargo/registry/ \
cd highscore-api && cargo install ${CARGO_INSTALL_OPTIONS} --locked --path .

FROM debian:12-slim AS final
RUN apt-get update && apt-get install -y ca-certificates tzdata && rm -rf /var/lib/apt/lists/*
ARG UID=10001
RUN adduser \
--disabled-password \
--home "/nonexistent" \
--shell "/sbin/nologin" \
--no-create-home \
--uid "${UID}" \
appuser
USER appuser
COPY --from=builder /usr/local/cargo/bin/visnake-highscore-api /usr/local/bin/
EXPOSE 3000
CMD ["visnake-highscore-api"]

0 comments on commit 809f788

Please sign in to comment.