Dockerfile Essentials

Write small, cache-friendly Dockerfiles that build fast.

  • dockerfile
  • docker layers
  • npm ci docker
  • EXPOSE
  • CMD ENTRYPOINT

Dockerfile Essentials

Write small, cache-friendly Dockerfiles that build fast.

Commands

CommandWhat it does
FROM node:22-alpineStart from a minimal base image
COPY package*.json ./ + RUN npm ci --omit=devInstall deps before copying source (cache layer)
COPY . .Copy application source last
EXPOSE 8080Document the listening port
CMD ["node", "server.js"]Default container command (exec form)

Example

FROM node:22-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 8080
CMD ["node", "server.js"]

FAQ

Why copy package.json first?

Dependency layers rebuild only when manifests change; source edits then reuse the cached npm layer.

CMD vs ENTRYPOINT?

CMD is the default args (easily overridden); ENTRYPOINT fixes the executable. Combine both for CLI images.

Întrebări frecvente

Why copy package.json first?
Dependency layers rebuild only when manifests change; source edits then reuse the cached npm layer.
CMD vs ENTRYPOINT?
CMD is the default args (easily overridden); ENTRYPOINT fixes the executable. Combine both for CLI images.

Docker