Docker Cheat Sheet

Essential Docker commands in one page. Images, containers, volumes, networks, Docker Compose, and troubleshooting — the commands you use daily.

Images Containers Volumes Networks Docker Compose Dockerfile Instructions Cleanup & Troubleshooting

Images

docker build -t myapp . Build image from Dockerfile in current dir
docker build -t myapp:v2 -f Dockerfile.prod . Build with specific Dockerfile and tag
docker images List all local images
docker pull nginx:alpine Pull image from Docker Hub
docker push user/myapp:v1 Push image to registry
docker rmi image_id Remove an image
docker image prune Remove unused images
docker tag myapp user/myapp:v1 Tag image for registry push

Containers

docker run -d -p 8080:80 nginx Run detached, map port 8080→80
docker run -it ubuntu bash Run interactive shell
docker run --rm myapp Run and auto-remove on exit
docker run -v ./data:/app/data myapp Mount host directory
docker run -e API_KEY=secret myapp Set environment variable
docker run --name web -d nginx Run with a custom name
docker ps List running containers
docker ps -a List all containers (including stopped)
docker stop container_id Gracefully stop a container
docker rm container_id Remove a stopped container
docker exec -it container_id bash Open shell in running container
docker logs -f container_id Follow container logs (live)

Volumes

docker volume create mydata Create a named volume
docker volume ls List all volumes
docker run -v mydata:/app/data myapp Mount named volume
docker volume rm mydata Remove a volume
docker volume prune Remove all unused volumes

Networks

docker network create mynet Create a custom network
docker network ls List all networks
docker run --network mynet myapp Connect container to network
docker network connect mynet container_id Add running container to network
docker network inspect mynet Show network details and connected containers

Docker Compose

docker compose up -d Start all services (detached)
docker compose down Stop and remove all services
docker compose down -v Stop, remove services and volumes
docker compose build Build/rebuild all service images
docker compose logs -f web Follow logs for "web" service
docker compose exec web bash Shell into running service
docker compose ps List running compose services
docker compose pull Pull latest images for all services
docker compose up -d --build Rebuild and restart all services

Dockerfile Instructions

FROM node:20-alpine Base image
WORKDIR /app Set working directory
COPY package*.json ./ Copy files into image
RUN npm ci --production Run command during build
EXPOSE 3000 Document exposed port
ENV NODE_ENV=production Set environment variable
CMD ["node", "server.js"] Default command when container starts
ENTRYPOINT ["node"] Fixed executable (CMD becomes args)
HEALTHCHECK CMD curl -f http://localhost/ Container health check
.dockerignore File listing patterns to exclude from COPY

Cleanup & Troubleshooting

docker system prune -a Remove all unused images, containers, networks
docker system df Show Docker disk usage
docker inspect container_id Detailed container/image info (JSON)
docker stats Live resource usage for all containers
docker top container_id Show processes in container
docker cp container_id:/path ./local Copy file from container to host

Try It Live

Test these patterns with our free Docker Compose Generator. No signup needed.

Open Docker Compose Generator →

More Cheat Sheets