Docker
Writing a Dockerfile
Build an image from a Dockerfile with multi-instruction layers.
By EZ4Code Team
dockerfilebuildimage
Code
# Dockerfile
FROM node:20-alpine AS base
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["node", "dist/server.js"]
# Build and tag
# docker build -t myapp:1.0 .
# docker history myapp:1.0 # inspect layersExplanation
Each instruction in a Dockerfile creates a layer; ordering COPY/RUN to leverage the build cache speeds up rebuilds. FROM sets the base image, WORKDIR sets the working directory, and CMD defines the default command. EXPOSE documents the port; it does not publish it.
More Docker Snippets
Run a Container
Run, list, stop, and remove Docker containers.
Image Management
Pull, list, tag, push, and prune images.
Volumes & Bind Mounts
Persist data with named volumes, anonymous volumes, and bind mounts.
Networking
Create networks, attach containers, and expose ports.
Docker Compose
Define and run multi-container apps with compose.
Multi-Stage Build
Build artifacts in one stage and copy them into a slim final image.