Devops

Docker Cheatsheet

Docker is a containerization platform that packages applications into portable containers. Master these commands for consistent deployments.

Docker is a containerization platform that packages applications and their dependencies into lightweight, portable containers. Containers run consistently across different environments, solving "it works on my machine" problems.

NOTE: Tested using the latest version at time of writing, v24+

Images

Build, list, and manage Docker images.

docker build -t <name> .        # Build image from Dockerfile
docker images                   # List images
docker rmi <image>              # Remove image
docker pull <image>             # Download image from registry
docker push <image>             # Upload image to registry

Containers

Create, start, stop, and manage containers.

docker run <image>              # Run container
docker run -d <image>            # Run in detached mode
docker run -p 3000:3000 <image> # Map ports
docker run -v /path:/path <img> # Mount volume
docker ps                       # List running containers
docker ps -a                    # List all containers
docker start <container>        # Start container
docker stop <container>         # Stop container
docker rm <container>           # Remove container
docker logs <container>         # View container logs

Dockerfile Basics

Create images with Dockerfile.

FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "index.js"]

Docker Compose

Define multi-container applications.

# docker-compose.yml
version: '3.8'
services:
  web:
    build: .
    ports:
      - "3000:3000"
  db:
    image: postgres:15
    environment:
      POSTGRES_PASSWORD: password
docker-compose up                # Start services
docker-compose up -d             # Detached mode
docker-compose down              # Stop services
docker-compose logs              # View logs
docker-compose ps                # List services

Networking

Connect containers and expose ports.

docker network ls                # List networks
docker network create <name>     # Create network
docker run --network <name>      # Connect to network
docker run -p host:container     # Port mapping

Volumes

Persist data with volumes.

docker volume ls                 # List volumes
docker volume create <name>      # Create volume
docker run -v <name>:/path       # Mount named volume
docker run -v /host:/container   # Bind mount

Environment Variables

Pass environment variables to containers.

docker run -e KEY=value <image>  # Single variable
docker run --env-file .env <img> # From file

Executing Commands

Run commands in running containers.

docker exec <container> <cmd>    # Execute command
docker exec -it <container> sh   # Interactive shell
docker exec -it <container> bash # Bash shell

Image Layers

Understand image layers and caching.

# Order matters for caching!
# Copy dependencies first (changes less)
COPY package.json .
RUN npm install

# Copy source code last (changes more)
COPY . .

Multi-Stage Builds

Reduce final image size.

# Build stage
FROM node:18 AS builder
WORKDIR /app
COPY . .
RUN npm run build

# Production stage
FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY package.json .
RUN npm install --production
CMD ["node", "dist/index.js"]

Health Checks

Monitor container health.

HEALTHCHECK --interval=30s --timeout=3s \
  CMD curl -f http://localhost:3000/health || exit 1

Resource Limits

Control container resources.

docker run --memory="512m" <img>  # Memory limit
docker run --cpus="1.5" <img>     # CPU limit

Cleanup

Remove unused resources.

docker system prune              # Remove unused data
docker system prune -a           # Remove all unused images
docker volume prune              # Remove unused volumes

Docker simplifies deployment and ensures consistency. Start with basic commands, then explore Docker Compose for multi-container apps.

For full documentation, see https://docs.docker.com

Promote your content

Reach over 400,000 developers and grow your brand.

Join our developer community

Hang out with over 4,500 developers and share your knowledge.