do-dockerfile
When to use
You need a production Dockerfile, a bloated image slimmed down, a slow build made cache-friendly, or a docker-compose stack for local development and integration testing.
Not for: Kubernetes manifests or runtime orchestration (use do-kubernetes), CI wiring that builds the image (use do-ci-pipeline), or host provisioning (use do-terraform).
Method
- Pick the base. Decision: use distroless or a digest-pinned slim tag for compiled/static apps; use alpine or slim only when you need a shell/package manager. Never
latest.
- Use multi-stage builds: a build stage with compilers and dev dependencies, a final stage that copies only the runtime artifact.
- Order layers by change frequency — copy the lockfile and install dependencies before copying source, so code edits do not bust the dependency cache.
- Harden. Decision: run as a non-root
USER; if the app writes at runtime, mount a writable volume rather than dropping readOnlyRootFilesystem. Avoid embedding secrets (use build args or mounts), and add a HEALTHCHECK.
- Trim: combine
RUN steps, clean package caches in the same layer, and add a .dockerignore to keep the build context lean.
- For compose: declare health-gated
depends_on, named volumes, and an isolated network; keep dev-only overrides in a separate file.
Example
# build stage — has the toolchain
FROM node:20-slim AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci # cached until lockfile changes
COPY . .
RUN npm run build
# runtime stage — minimal, non-root
FROM gcr.io/distroless/nodejs20-debian12
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY --from=build /app/node_modules ./node_modules
USER nonroot
HEALTHCHECK CMD ["node", "dist/healthcheck.js"]
CMD ["dist/server.js"]
Pitfalls
- Copying source before installing dependencies, so every code edit reinstalls everything from scratch.
- Running as root in the runtime image — a container escape becomes host root.
- Using
ADD with a remote URL or leaving a build-arg secret in an intermediate layer; docker history exposes it.
- Cleaning apt/npm caches in a separate
RUN — the deleted files still live in the earlier layer, so the image never shrinks.
Output format
Dockerfile: multi-stage, commented.
.dockerignore: entries listed.
docker-compose.yml: when relevant (health-gated deps, named volumes).
Before/after:
- image size: <old> -> <new>
- layer cache: <what now stays cached on a code-only edit>
- security: <non-root? secrets removed? healthcheck added?>
1---2name: do-dockerfile3version: 1.0.04description: Write or optimize a Dockerfile and compose stack for small, secure, cache-friendly, reproducible images using multi-stage builds and non-root runtime.5author: matrixx00706tags: [docker, containers, devops, security, build, compose]7capabilities: []8metadata:9 source: https://skillmd.com/skills/matrixx0070/do-dockerfile10---1112# do-dockerfile1314## When to use1516You need a production Dockerfile, a bloated image slimmed down, a slow build made cache-friendly, or a docker-compose stack for local development and integration testing.1718**Not for:** Kubernetes manifests or runtime orchestration (use do-kubernetes), CI wiring that builds the image (use do-ci-pipeline), or host provisioning (use do-terraform).1920## Method21221. Pick the base. Decision: use distroless or a digest-pinned slim tag for compiled/static apps; use alpine or slim only when you need a shell/package manager. Never `latest`.232. Use multi-stage builds: a build stage with compilers and dev dependencies, a final stage that copies only the runtime artifact.243. Order layers by change frequency — copy the lockfile and install dependencies before copying source, so code edits do not bust the dependency cache.254. Harden. Decision: run as a non-root `USER`; if the app writes at runtime, mount a writable volume rather than dropping `readOnlyRootFilesystem`. Avoid embedding secrets (use build args or mounts), and add a `HEALTHCHECK`.265. Trim: combine `RUN` steps, clean package caches in the same layer, and add a `.dockerignore` to keep the build context lean.276. For compose: declare health-gated `depends_on`, named volumes, and an isolated network; keep dev-only overrides in a separate file.2829## Example3031```dockerfile32# build stage — has the toolchain33FROM node:20-slim AS build34WORKDIR /app35COPY package*.json ./36RUN npm ci # cached until lockfile changes37COPY . .38RUN npm run build3940# runtime stage — minimal, non-root41FROM gcr.io/distroless/nodejs20-debian1242WORKDIR /app43COPY --from=build /app/dist ./dist44COPY --from=build /app/node_modules ./node_modules45USER nonroot46HEALTHCHECK CMD ["node", "dist/healthcheck.js"]47CMD ["dist/server.js"]48```4950## Pitfalls5152- Copying source before installing dependencies, so every code edit reinstalls everything from scratch.53- Running as root in the runtime image — a container escape becomes host root.54- Using `ADD` with a remote URL or leaving a build-arg secret in an intermediate layer; `docker history` exposes it.55- Cleaning apt/npm caches in a separate `RUN` — the deleted files still live in the earlier layer, so the image never shrinks.5657## Output format5859```60Dockerfile: multi-stage, commented.61.dockerignore: entries listed.62docker-compose.yml: when relevant (health-gated deps, named volumes).6364Before/after:65- image size: <old> -> <new>66- layer cache: <what now stays cached on a code-only edit>67- security: <non-root? secrets removed? healthcheck added?>68```