| Command | Description |
|---|---|
docker run -d -p 80:80 --name web nginx |
Run container in background (-d), map ports (-p host:container), set name |
docker ps |
List active running containers |
docker ps -a |
List all containers (including stopped ones) |
docker stop <id/name> |
Stop a running container |
docker rm <id/name> |
Delete a stopped container |
docker logs -f <id/name> |
Follow logs for a container in real time |
docker exec -it <id/name> sh |
Execute an interactive terminal session (-it) inside a container |
docker images |
List all locally stored images |
docker rmi <image> |
Delete an image |
docker system prune -a --volumes |
Clean up unused containers, networks, images, and volumes |
# 1. Use specific base images (avoid :latest)
FROM node:20-alpine AS builder
WORKDIR /app
# 2. Leverage layer caching by copying lockfile first
COPY package*.json ./
RUN npm ci
# 3. Copy source code and build
COPY . .
RUN npm run build
# 4. Multi-stage build to minimize production image size
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY --from=builder /app/dist ./dist
# 5. Security: Run as a non-root user
USER node
EXPOSE 3000
CMD ["node", "dist/index.js"]
| Command | Description |
|---|---|
docker compose up -d |
Build, (re)create, and start containers in the background |
docker compose down |
Stop and remove containers, networks, images, and volumes |
docker compose ps |
List container status, ports, and health status |
docker compose logs -f api |
Stream logs from a specific service |
docker compose exec db psql -U user |
Run commands directly inside a running service container |