Skip to content

Commit

Permalink
Improve Dockerfile image layers to avoid recompiles
Browse files Browse the repository at this point in the history
  • Loading branch information
jbuckmccready committed Aug 10, 2023
1 parent b1ae333 commit e84d846
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
# syntax=docker/dockerfile:1

# Comments are provided throughout this file to help you get started.
# If you need more help, visit the Dockerfile reference guide at
# https://docs.docker.com/engine/reference/builder/

################################################################################
# Create a stage for building the application.

# Stage for building backend
ARG RUST_VERSION=1.71.0
ARG APP_NAME=cavalier_contours_server
FROM rust:${RUST_VERSION}-slim-bullseye AS build_backend
ARG APP_NAME
WORKDIR /app

# Build the application.
# Leverage a cache mount to /usr/local/cargo/registry/
Expand All @@ -30,39 +26,36 @@ cargo build --locked --release
cp ./target/release/$APP_NAME /bin/server
EOF

# Stage for building frontend (Rust needed for building WASM)
FROM rust:${RUST_VERSION}-slim-bullseye AS build_frontend
WORKDIR /app

# Install curl
RUN apt-get update && \
apt-get install -yq --no-install-recommends \
curl

# Install node and wasm-pack
RUN <<EOF
curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
apt-get install -y nodejs
npm install -g wasm-pack
EOF

# Get dependencies
COPY ["frontend/package.json", "frontend/package-lock.json*", "./"]
RUN npm install

# Build wasm
COPY ["frontend/cavalier_contours_web_ffi/", "./cavalier_contours_web_ffi"]
RUN npm run wasm

# Vite build
COPY ["frontend/", "./"]
RUN npm run build && cp -r ./dist /bin/index

RUN <<EOF
npm install
npm run wasm
npm run build
cp -r ./dist /bin/index
EOF

################################################################################
# Create a new stage for running the application that contains the minimal
# runtime dependencies for the application. This often uses a different base
# image from the build stage where the necessary files are copied from the build
# stage.
#
# The example below uses the debian bullseye image as the foundation for running the app.
# By specifying the "bullseye-slim" tag, it will also use whatever happens to be the
# most recent version of that tag when you build your Dockerfile. If
# reproducability is important, consider using a digest
# (e.g., debian@sha256:ac707220fbd7b67fc19b112cee8170b41a9e97f703f588b2cdbbcdcecdd8af57).
# Final stage for minimal runtime
FROM debian:bullseye-slim AS final

# Create a non-privileged user that the app will run under.
Expand All @@ -78,7 +71,7 @@ RUN adduser \
appuser
USER appuser

# Copy the executable from the "build" stage.
# Copy files from build stages
COPY --from=build_backend /bin/server /bin/
COPY --from=build_frontend /bin/index /bin/index

Expand Down

0 comments on commit e84d846

Please sign in to comment.