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
| Command | What it does |
|---|---|
FROM node:22-alpine | Start from a minimal base image |
COPY package*.json ./ + RUN npm ci --omit=dev | Install deps before copying source (cache layer) |
COPY . . | Copy application source last |
EXPOSE 8080 | Document 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.
Preguntas frecuentes
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
Docker Install and Setup
Verify the Docker engine, configure context, and check versions.
Docker Images
Pull, list, tag, inspect, and remove images confidently.
Docker Containers
Run, list, stop, and clean up containers in daily work.
Docker Run Options
The flags that matter: detach, ports, env, volumes, limits.