Skip to content

Commit

Permalink
parametric docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
ships committed Feb 1, 2024
1 parent db06834 commit cd49d0d
Showing 1 changed file with 14 additions and 17 deletions.
31 changes: 14 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
ARG NODE_VERSION=20.6.0
ARG PNPM_VERSION=8.14.3

ARG PACKAGE_DIR=core
ARG PACKAGE=core

################################################################################
# Use node image for base image for all stages.
FROM node:${NODE_VERSION}-alpine as base
Expand All @@ -24,14 +27,15 @@ RUN --mount=type=cache,target=/root/.npm \
################################################################################
# Create a stage for installing production dependecies.
FROM base as deps
ARG PACKAGE_DIR

# Download dependencies as a separate step to take advantage of Docker's caching.
# Leverage a cache mount to /root/.local/share/pnpm/store to speed up subsequent builds.
# Leverage bind mounts to package.json and pnpm-lock.yaml to avoid having to copy them
# into this layer.
RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=pnpm-workspace.yaml,target=pnpm-workspace.yaml \
--mount=type=bind,source=core/package.json,target=core/package.json \
--mount=type=bind,source=${PACKAGE_DIR}/package.json,target=${PACKAGE_DIR}/package.json \
--mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \
--mount=type=cache,target=/root/.local/share/pnpm/store \
--mount=type=cache,target=/pnpm/store \
Expand All @@ -40,11 +44,12 @@ RUN --mount=type=bind,source=package.json,target=package.json \
################################################################################
# Create a stage for building the application.
FROM deps as build
ARG PACKAGE_DIR PACKAGE

# Download additional development dependencies before building, as some projects require
# "devDependencies" to be installed to build. If you don't need this, remove this step.
RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=core/package.json,target=core/package.json \
--mount=type=bind,source=${PACKAGE_DIR}/package.json,target=${PACKAGE_DIR}/package.json \
--mount=type=bind,source=pnpm-lock.yaml,target=pnpm-lock.yaml \
--mount=type=cache,target=/root/.local/share/pnpm/store \
--mount=type=cache,target=/pnpm/store \
Expand All @@ -54,12 +59,13 @@ RUN --mount=type=bind,source=package.json,target=package.json \
COPY . .

# Run the build script.
RUN ./bin/render-build.sh core
RUN ./bin/render-build.sh ${PACKAGE}

################################################################################
# Create a new stage to run the application with minimal runtime dependencies
# where the necessary files are copied from the build stage.
FROM base as final
ARG PACKAGE_DIR PACKAGE

# Use production node environment by default.
ENV NODE_ENV production
Expand All @@ -72,28 +78,19 @@ COPY package.json \
pnpm-workspace.yaml \
.

COPY ./core/package.json \
./core/.env.template \
./core/postcss.config.js \
./core/tailwind.config.js \
./core/

# TODO: These may be needed, but are TS currently:
# ./core/next.config.js \
# ./core/sentry.client.config.ts \
# ./core/sentry.edge.config.ts \
# ./core/sentry.server.config.ts \
COPY ./${PACKAGE_DIR}/package.json \
./${PACKAGE_DIR}/

# Copy the production dependencies from the deps stage and also
# the built application from the build stage into the image.

COPY --from=deps /usr/src/app/node_modules ./node_modules
COPY --from=build /usr/src/app/core/node_modules ./core/node_modules
COPY --from=build /usr/src/app/core/.next ./core/.next
COPY --from=build /usr/src/app/${PACKAGE_DIR}/node_modules ./${PACKAGE_DIR}/node_modules
COPY --from=build /usr/src/app/${PACKAGE_DIR}/.next ./${PACKAGE_DIR}/.next


# Expose the port that the application listens on.
EXPOSE 3000

# Run the application.
CMD pnpm --filter core start
CMD pnpm --filter ${PACKAGE} start

0 comments on commit cd49d0d

Please sign in to comment.