Docker & Dockerfile Cheat Sheet — Essential CLI Reference

High-frequency Docker commands for building container images, managing networks, volumes, debugging logs, and cleaning disk space.

Container Management

docker ps

List all running containers with IDs, image names, and port mappings

docker ps -a

List all containers including stopped and exited ones

docker run -d -p 8080:80 --name myapp nginx

Run container in background (-d) forwarding host 8080 to container 80

docker stop <container_id>

Gracefully stop a running container (SIGTERM followed by SIGKILL)

docker restart <container_id>

Restart a container immediately

docker rm -f <container_id>

Force remove a running or stopped container

docker exec -it <container_id> sh

Open an interactive shell inside a running container

Image Building & Registry

docker build -t myapp:1.0 .

Build an image from Dockerfile in current directory with tag myapp:1.0

docker images

List all local container images and their sizes

docker rmi <image_id>

Delete a local Docker image from disk

docker pull <image_name>

Download latest image from Docker Hub or registry

Logs & Debugging

docker logs -f --tail 100 <container_id>

Stream live logs (-f) showing last 100 lines

docker inspect <container_id>

Show detailed JSON low-level configuration of container or image

docker stats

Display live CPU, memory, and network I/O usage per container

Disk Cleanup & System Pruning

docker system prune -a --volumes

Reclaim gigabytes by removing stopped containers, unused networks, dangling images, and volumes

docker image prune

Remove all untagged/dangling images

docker volume prune

Remove all anonymous volumes not in use by any container