Optimize Docker images with multi-stage builds
Reduces Docker image size by 30-50% using multi-stage builds and production-only dependencies, based on Docker image optimization best practices.
Changes
Dockerfiles (.docker/alpine/Dockerfile, .docker/debian/Dockerfile)
- Multi-stage build: Separate builder stage (compile TypeScript with all deps) from production stage (runtime only)
-
Layer caching: Copy
package.json→ install → copy source (improves build cache hit rate) -
Production dependencies: Use
npm ci --only=productionin final stage, exclude ~50-100MB of devDependencies - Artifact cleanup: Consolidate npm cache clean and temp removal in single RUN layer
- Signal handling: Add dumb-init for proper SIGTERM propagation
- Selective copy: Only dist/, config/, sql/, and HTML templates in production image
.dockerignore
Expand exclusions from 7 to 30+ patterns: development files, documentation, tests, CI/CD configs, IDE settings
Structure
# Builder stage - all dependencies + TypeScript compilation
FROM node:22-alpine AS builder
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run tsc
# Production stage - runtime only
FROM node:22-alpine
RUN apk add --no-cache dumb-init
COPY package*.json ./
RUN npm ci --only=production && npm cache clean --force && rm -rf /root/.npm /tmp/*
COPY --from=builder /usr/src/app/dist ./dist
COPY config ./config
COPY sql ./sql
COPY app/port/*.html ./app/port/
ENTRYPOINT ["dumb-init", "--"]
CMD ["npm", "run", "start:foreground"]
Size Reduction Breakdown
| Component | Savings | Details |
|---|---|---|
| DevDependencies | ~50-100MB | TypeScript compiler, test frameworks, type definitions |
| Source files | ~10-20MB | .ts files, test directory |
| Build artifacts | ~5-10MB | npm cache, temporary files |
Documentation
-
DOCKER_OPTIMIZATION.md: Detailed optimization rationale and comparison -
.docker/README.md: Build and test instructions -
docs/deploy-in-docker.md: Added optimization reference
Backward Compatibility
No breaking changes. Environment variables, ports, and startup behavior unchanged. Existing deployment scripts remain compatible.
Original prompt
This section details on the original issue you should resolve
<issue_title>reduce docker image size</issue_title> <issue_description>https://blog.prateekjain.dev/a-step-by-step-guide-to-docker-image-optimisation-reduce-size-by-over-95-d90bcab3819d</issue_description>
<agent_instructions>Experience from the reference document https://blog.prateekjain.dev/a-step-by-step-guide-to-docker-image-optimisation-reduce-size-by-over-95-d90bcab3819d</agent_instructions>
Comments on the Issue (you are @copilot in this section)
- Fixes cnpm/cnpmcore#887
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
How to use the Graphite Merge Queue
Add either label to this PR to merge it via the merge queue:
- MQ - adds this PR to the back of the merge queue
- Hotfix - for urgent hot fixes, skip the queue and merge this PR next
You must have a Graphite account in order to use the merge queue. Sign up using this link.
An organization admin has enabled the Graphite Merge Queue in this repository.
Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.
[!IMPORTANT]
Review skipped
Bot user detected.
To trigger a single review, invoke the
@coderabbitai reviewcommand.You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.
Comment @coderabbitai help to get the list of available commands and usage tips.
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 95.19%. Comparing base (93034bf) to head (51912b7).
:warning: Report is 3 commits behind head on master.
Additional details and impacted files
@@ Coverage Diff @@
## master #888 +/- ##
========================================
Coverage 95.19% 95.19%
========================================
Files 197 197
Lines 23948 23948
Branches 2496 2314 -182
========================================
Hits 22797 22797
Misses 1151 1151
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.