chore(docker): shrink image with multi-stage slim build#476
Open
RodriSanchez1 wants to merge 1 commit into
Open
chore(docker): shrink image with multi-stage slim build#476RodriSanchez1 wants to merge 1 commit into
RodriSanchez1 wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Reworks the
Dockerfileto significantly reduce the final image size using a multi-stage build.Changes
node:18.18.1→node:18.18.1-slim(the biggest size win).depsstage: installs production dependencies only (--production --frozen-lockfile) and runsyarn cache clean.runtimestage: copies onlynode_modulesfrom thedepsstage, leaving build-time cruft out of the shipped image. Also improves rebuild speed via layer caching (deps are installed before source is copied).node-gyp,swagger) — neither is needed at runtime (bcryptjsis pure JS;req.swaggercomes from theswagger-*dependencies, not the global CLI).nodeuser and starts vianode app.jsdirectly instead ofyarn start..dockerignore: replaced the broken un-ignore rules with a real exclude list, sonode_modules,.git, tests, etc. no longer leak into the build context.Testing
docker build -t cboard-api:slim .completes successfullydocker images cboard-api:slimshows a reduced size vs the previous full image🤖 Generated with Claude Code