Skip to content

Docker

🐋 Docker CLI Cheatsheet

🏗️ Containers (Lifecycle)

Command Description
docker run <image> Create and start a container from an image
docker run -d <image> Run container in background (detached)
docker ps List running containers
docker ps -a List all containers (including stopped)
docker stop <ID> Gracefully stop a container
docker rm <ID> Remove a container
docker exec -it <ID> bash Open a terminal inside a running container

🖼️ Images

Command Description
docker images List all locally stored images
docker build -t <name> . Build an image from a Dockerfile in current directory
docker pull <image> Download an image from Docker Hub
docker rmi <image> Delete a local image
docker system prune Clean up: Remove unused data (containers, networks, images)

📂 Volumes & Networking

Command Description
docker volume ls List all volumes
docker volume create <name> Create a managed volume for persistent data
docker network ls List all networks
docker run -p 8080:80 Map host port 8080 to container port 80

🚢 Docker Compose

Command Description
docker-compose up Build, create, and start containers defined in .yml
docker-compose up -d Run Compose in the background
docker-compose down Stop and remove containers, networks, and images
docker-compose logs -f View live output from services

Quick Cleanup

To stop and remove all containers at once, use: docker rm -f $(docker ps -aq)

Use with Caution

docker system prune -a will delete ALL unused images, not just dangling ones.