A quick-reference guide for the docker commands you’ll reach for most often to build, run, and manage containers, images, volumes, and networks.
Table of Contents
- Images
- Running Containers
- Managing Containers
- Logs and Exec
- Volumes
- Networks
- Docker Compose
- System and Cleanup
Images
# List local images
docker images
# Pull an image from a registry
docker pull nginx:latest
# Build an image from a Dockerfile in the current directory
docker build -t myapp:1.0 .
# Tag an image for a registry
docker tag myapp:1.0 myrepo/myapp:1.0
# Push an image to a registry
docker push myrepo/myapp:1.0
# Remove an image
docker rmi myapp:1.0
# Remove all unused images
docker image prune -a
Running Containers
# Run a container in the background with a published port
docker run -d --name web -p 8080:80 nginx
# Run a container and drop into an interactive shell
docker run -it ubuntu bash
# Run a container and remove it automatically when it exits
docker run --rm alpine echo "hello"
# Run a container with an environment variable set
docker run -e APP_ENV=production myapp
# Run a container with a mounted named volume
docker run -v myvolume:/data myapp
Managing Containers
# List running containers
docker ps
# List all containers, including stopped ones
docker ps -a
# Stop a running container
docker stop web
# Start a stopped container
docker start web
# Restart a container
docker restart web
# Remove a container
docker rm web
# Remove all stopped containers
docker container prune
Logs and Exec
# Stream logs from a container
docker logs -f web
# Run a command inside a running container
docker exec -it web sh
# Show live resource usage for running containers
docker stats
# Inspect the full configuration of a container
docker inspect web
Volumes
# List volumes
docker volume ls
# Create a named volume
docker volume create myvolume
# Inspect a volume
docker volume inspect myvolume
# Remove a volume
docker volume rm myvolume
# Remove all unused volumes
docker volume prune
Networks
# List networks
docker network ls
# Create a custom bridge network
docker network create mynet
# Connect a running container to a network
docker network connect mynet web
# Inspect a network
docker network inspect mynet
# Remove a network
docker network rm mynet
Docker Compose
# Start all services defined in compose.yaml
docker compose up -d
# Rebuild images before starting services
docker compose up -d --build
# View logs for all services
docker compose logs -f
# List running compose services
docker compose ps
# Stop and remove services, networks, and volumes
docker compose down
System and Cleanup
# Show disk usage by images, containers, and volumes
docker system df
# Remove all unused containers, networks, and images
docker system prune -a
# Show Docker version and build information
docker version
# Show system-wide information
docker info
