Docker Tutorial
Containerize an app — images, Dockerfile, volumes, and docker-compose.
Docker packages your app and its dependencies into a portable image that runs the same everywhere.
A minimal Dockerfile
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
Build and run:
docker build -t myapp .
docker run -p 3000:3000 myapp
Layers and caching
Order your Dockerfile so the least-changing steps come first. COPY package*.json + npm ci before copying source means code changes don’t invalidate the dependency layer.
Persistent data with volumes
docker volume create appdata
docker run -v appdata:/data myapp
Multi-container with compose
# docker-compose.yml
services:
web:
build: .
ports: ["3000:3000"]
db:
image: postgres:16
environment:
POSTGRES_PASSWORD: secret
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
Run everything with docker compose up.
Keep images small: prefer
-alpinebases and multi-stage builds for compiled languages.