Loading...
A dense, no-fluff Docker reference covering core concepts, Dockerfile syntax, build/run commands, Compose, volumes, multi-stage builds, and production hardening. One page, everything you need.
FROM node:20-alpine # base image
WORKDIR /app # set working dir
COPY package*.json ./ # copy deps first (cache optimization)
RUN npm ci --omit=dev # install deps
COPY . . # copy source
EXPOSE 3000 # document port (does NOT publish)
CMD ["node", "server.js"] # default command
| Instruction | Use |
|---|---|
ARG | build-time variable |
ENV | runtime environment variable |
ENTRYPOINT | fixed executable, CMD becomes args |
HEALTHCHECK | defines container health probe |
| Command | Description |
|---|---|
docker build -t name:tag . | build image from current dir |
docker build --no-cache -t name . | force rebuild all layers |
docker run -d -p 3000:3000 name | run detached, map host:container port |
docker run --rm -it name sh | interactive one-off container |
docker run -e KEY=val name | pass environment variable |
docker run -v $(pwd):/app name | bind mount current dir |
| Command | Description |
|---|---|
docker ps | list running containers |
docker ps -a | list all including stopped |
docker stop <id> | graceful SIGTERM stop |
docker kill <id> | immediate SIGKILL |
docker rm <id> | remove stopped container |
docker exec -it <id> sh | shell into running container |
docker logs -f <id> | stream container logs |
docker inspect <id> | full JSON metadata |
| Command | Description |
|---|---|
docker images | list local images |
docker pull name:tag | pull from registry |
docker push name:tag | push to registry |
docker tag src:tag dest:tag | create alias tag |
docker rmi <id> | remove image |
docker image prune -a | remove all unused images |
docker save name > file.tar | export image to tar |
docker load < file.tar | import image from tar |
# compose.yaml
services:
app:
build: .
ports: ["3000:3000"]
env_file: .env
depends_on: [db]
volumes:
- .:/app
db:
image: postgres:16-alpine
environment:
POSTGRES_PASSWORD: secret
volumes:
- pg_data:/var/lib/postgresql/data
volumes:
pg_data:
| Command | Description |
|---|---|
docker compose up -d | start all services detached |
docker compose down -v | stop and remove volumes |
docker compose logs -f app | tail specific service logs |
docker compose exec app sh | shell into running service |
# Named volume
docker volume create mydata
docker run -v mydata:/data image
# Bind mount
docker run -v /host/path:/container/path image
# Networks
docker network create mynet
docker run --network mynet --name api image
docker run --network mynet image curl http://api:3000
bridge — default isolated networkhost — shares host network stack (Linux only)# Stage 1: build
FROM node:20-alpine AS builder
WORKDIR /app
COPY . .
RUN npm ci && npm run build
# Stage 2: production image
FROM node:20-alpine AS runner
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
CMD ["node", "dist/index.js"]
| Command | Description |
|---|---|
docker system prune | remove stopped containers, dangling images, unused networks |
docker system prune -a | above + all unused images |
docker volume prune | remove all unused volumes |
docker image prune --filter "until=24h" | remove images older than 24h |
Run
docker system dffirst to see disk usage before pruning.
node:20-alpine or distroless — not latest or full Debianimage@sha256:abc123USER node before CMD--read-only flag on containers where possible.env or secrets into the image; use --env-file or Docker secrets.dockerignore — exclude node_modules, .git, .env, distnode_modules
.git
.env
*.log
dist