Skip to content

Commit

Permalink
Merge pull request #69 from Hilzu/chore/simplify-docker-image
Browse files Browse the repository at this point in the history
Simplify Dockerfile and use explicit commands to install production deps
  • Loading branch information
Hilzu authored Jan 10, 2025
2 parents dd39f3e + 1d31bc1 commit ea93e8d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
7 changes: 5 additions & 2 deletions src/main.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ export const create = async ({ projectName, projectPath, packageManager }) => {
: pmName === "pnpm" ? "pnpm-lock.yaml"
: "yarn.lock";

const pmInstall = pmName === "npm" ? "npm ci" : `${pmName} install`;
const pmInstall =
pmName === "npm" ? "npm ci" : `${pmName} install --frozen-lockfile`;
const pmInstallProd = `${pmInstall} ${pmName === "npm" ? "--omit=dev" : "--prod"}`;

const pmDockerCacheDir =
pmName === "npm" ? "/root/.npm"
Expand Down Expand Up @@ -147,7 +149,8 @@ export const create = async ({ projectName, projectPath, packageManager }) => {
const dockerfilePath = pathJoin(projectPath, "Dockerfile");
let dockerfile = await readFile(dockerfilePath, { encoding: "utf-8" });
dockerfile = dockerfile
.replaceAll("PM_INSTALL", pmInstall)
.replaceAll("PM_INSTALL_DEV", pmInstall)
.replaceAll("PM_INSTALL_PROD", pmInstallProd)
.replaceAll("PM_LOCK_FILE", pmLockFile)
.replaceAll("PM_CACHE_DIR", pmDockerCacheDir)
.replaceAll(
Expand Down
18 changes: 11 additions & 7 deletions template/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,27 @@ FROM node:${NODE_VERSION} AS build
ENV NODE_ENV=development
WORKDIR /opt/build
INSTALL_PNPM
RUN --mount=type=bind,source=package.json,target=package.json \
--mount=type=bind,source=PM_LOCK_FILE,target=PM_LOCK_FILE \
--mount=type=cache,target=PM_CACHE_DIR,sharing=locked \
PM_INSTALL
COPY package.json PM_LOCK_FILE ./
RUN PM_INSTALL_DEV
COPY . .
RUN node --run build


FROM node:${NODE_VERSION} AS deps
ENV NODE_ENV=production
WORKDIR /opt/deps
INSTALL_PNPM
COPY package.json PM_LOCK_FILE ./
RUN PM_INSTALL_PROD


FROM node:${NODE_VERSION}-slim AS app
ENV NODE_ENV=production
ENV PORT=3000
WORKDIR /opt/app

COPY package.json PM_LOCK_FILE ./
INSTALL_PNPM
RUN --mount=type=cache,target=PM_CACHE_DIR,sharing=locked \
PM_INSTALL
COPY --from=deps /opt/deps/node_modules ./node_modules
COPY --from=build /opt/build/dist ./dist

USER node:node
Expand Down

0 comments on commit ea93e8d

Please sign in to comment.