Introduction

This cheatsheet provides a quick reference for the most common Docker commands.

Getting Started

# Run a test image to verify installation
docker run hello-world

# Display system-wide information
docker info

# Display Docker version information
docker version

Image Management

# List all images
docker images ls

# Pull an image from a registry (Docker Hub by default)
docker image pull alpine

# Push an image to a registry
docker image push your-username/your-image

# Build an image from a Dockerfile
# -t tags the image with a name
docker image build -t my-app .

# Remove an image
docker rmi my-image

# Save an image to a .tar file
docker save -o my-image.tar my-image

# Load an image from a .tar file
docker load -i my-image.tar

# Tag an image
docker tag source-image:tag target-image:tag

Container Management

# List running containers
docker ps
docker container ls

# List all containers (running and stopped)
docker ps -a
docker container ls -a

# Run a container from an image
# -d: detached mode (run in background)
# -p: publish a port (host:container)
# --name: assign a name
docker run -d -p 8080:80 --name my-web-server nginx

# Run a container in interactive mode
# -it: interactive TTY
docker run -it ubuntu bash

# Stop a running container
docker stop my-container

# Start a stopped container
docker start my-container

# Remove a stopped container
docker rm my-container

# Remove a running container (force)
docker rm -f my-container

# Remove all stopped containers
docker container prune

# View logs of a container
# -f: follow log output
docker logs -f my-container

# Execute a command in a running container
docker exec -it my-container bash

Docker Compose

# Build, create, and start services in detached mode
docker-compose up -d

# Stop and remove containers, networks, and volumes
docker-compose down

# List services
docker-compose ps

# View logs from all services
docker-compose logs -f

# Execute a command in a service container
docker-compose exec my-service bash

Docker Swarm

# Initialize a swarm
docker swarm init --advertise-addr <MANAGER-IP>

# Join a swarm as a worker
docker swarm join --token <TOKEN> <MANAGER-IP>:<PORT>

# List nodes in the swarm
docker node ls

# Create a service
docker service create --name my-service --replicas 3 -p 8080:80 nginx

# List services
docker service ls

# List tasks of a service
docker service ps my-service

# Scale a service
docker service scale my-service=5

# Deploy a stack from a compose file
docker stack deploy -c docker-compose.yml my-stack

Networking

# List networks
docker network ls

# Create a network
docker network create my-network

# Run a container on a specific network
docker run --network my-network ...

# Inspect a network
docker network inspect my-network

Volumes

# List volumes
docker volume ls

# Create a volume
docker volume create my-volume

# Run a container with a volume
docker run -v my-volume:/path/in/container ...

# Remove a volume
docker volume rm my-volume