Skip to content

Commit

Permalink
Optimize dev Dockerfile for rebuild speed and readonly filesystem
Browse files Browse the repository at this point in the history
  • Loading branch information
emlun committed Sep 19, 2023
1 parent 94327a5 commit 75ab425
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
9 changes: 7 additions & 2 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
.env
.git
.gitattributes
.gitignore
.npmrc.template
node_modules
.vscode
README.md
dist
.vscode
node_modules
38 changes: 26 additions & 12 deletions development.Dockerfile
Original file line number Diff line number Diff line change
@@ -1,20 +1,34 @@
FROM node:16-bullseye-slim
WORKDIR /home/node/app
FROM node:16-bullseye-slim as dependencies

# Copy package.json and yarn.lock and npmrc to the container
COPY package.json yarn.lock .npmrc ./
# RUN apt update -y && apt install python3 -y
WORKDIR /dependencies

RUN mkdir -p node_modules
# Install dependencies first so rebuild of these layers is only needed when dependencies change
COPY package.json yarn.lock .npmrc .
RUN yarn cache clean && yarn install --frozen-lockfile


FROM node:16-bullseye-slim as cli-dependencies

WORKDIR /dependencies

# Install dependencies first so rebuild of these layers is only needed when dependencies change
COPY cli/package.json cli/yarn.lock .
RUN --mount=type=secret,id=npmrc,required=true,target=./.npmrc,uid=1000 \
yarn cache clean && yarn install --frozen-lockfile


FROM node:16-bullseye-slim as development

COPY --from=cli-dependencies /dependencies/node_modules /cli_node_modules

ENV NODE_PATH=/node_modules
COPY --from=dependencies /dependencies/node_modules /node_modules

WORKDIR /app
ENV NODE_ENV development
CMD ["yarn", "dev-docker"]

RUN mkdir -p dist
RUN chown -R node:node /home/node/app/node_modules
RUN chown -R node:node /home/node/app/dist
# Set user last so everything is readonly by default
USER node
CMD ["yarn", "dev"]

# Copy the rest of the application code to the container
COPY --chown=node:node . .
# Don't need the rest of the sources since they'll be mounted from host
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
"test": "echo \"Error: no test specified\" && exit 1",
"build": "rm -rf dist && npx tsc",
"dev": "nodemon --config nodemon.json --exec \"npx tsc && yarn start\"",
"dev-docker": "nodemon --config nodemon.json --exec \"npx tsc --outdir /dist && yarn start-docker\"",
"start": "node -r source-map-support/register dist/src/app.js",
"start-docker": "node -r source-map-support/register /dist/src/app.js",
"typeorm": "typeorm-ts-node-commonjs -d src/AppDataSource.ts",
"typeorm-raw": "typeorm-ts-node-commonjs"
},
Expand Down

0 comments on commit 75ab425

Please sign in to comment.