Skip to content

chore(docker): shrink image with multi-stage slim build#476

Open
RodriSanchez1 wants to merge 1 commit into
masterfrom
chore/optimize-dockerfile-multistage
Open

chore(docker): shrink image with multi-stage slim build#476
RodriSanchez1 wants to merge 1 commit into
masterfrom
chore/optimize-dockerfile-multistage

Conversation

@RodriSanchez1

Copy link
Copy Markdown
Collaborator

Summary

Reworks the Dockerfile to significantly reduce the final image size using a multi-stage build.

Changes

  • Base image: node:18.18.1node:18.18.1-slim (the biggest size win).
  • deps stage: installs production dependencies only (--production --frozen-lockfile) and runs yarn cache clean.
  • runtime stage: copies only node_modules from the deps stage, leaving build-time cruft out of the shipped image. Also improves rebuild speed via layer caching (deps are installed before source is copied).
  • Removed the unused global installs (node-gyp, swagger) — neither is needed at runtime (bcryptjs is pure JS; req.swagger comes from the swagger-* dependencies, not the global CLI).
  • Security: runs as the non-root node user and starts via node app.js directly instead of yarn start.
  • .dockerignore: replaced the broken un-ignore rules with a real exclude list, so node_modules, .git, tests, etc. no longer leak into the build context.

Testing

  • docker build -t cboard-api:slim . completes successfully
  • docker images cboard-api:slim shows a reduced size vs the previous full image
  • Container starts and serves requests (requires a reachable MongoDB)

🤖 Generated with Claude Code

Rework the Dockerfile to reduce the final image size:

- Switch base image from node:18.18.1 to node:18.18.1-slim.
- Add a dedicated deps stage that installs production dependencies
  only (--production --frozen-lockfile) and cleans the yarn cache.
- Copy only node_modules from the deps stage into the runtime image,
  leaving build-time cruft behind.
- Drop the unused global installs (node-gyp, swagger); neither is
  needed at runtime.
- Run as the non-root node user and start via `node app.js` directly.
- Fix the broken .dockerignore so node_modules, .git, tests and other
  files are excluded from the build context.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant