D: 🚀 Whether you’re a beginner or an experienced developer, this Docker cheat sheet will be your ultimate guide! Bookmark this page for quick reference to the most essential Docker commands with real-world examples.
🔧 1. Container Lifecycle Management
▶️ Run a Container
docker run [OPTIONS] IMAGE [COMMAND]
- Example:
docker run -d -p 8080:80 --name my_nginx nginx
-d
: Detached mode (runs in background)-p 8080:80
: Maps host port 8080 → container port 80--name
: Assigns a custom name
⏹️ Stop/Kill Containers
docker stop CONTAINER_ID # Graceful shutdown
docker kill CONTAINER_ID # Force immediate stop
🔄 Restart/Remove
docker restart CONTAINER_ID
docker rm CONTAINER_ID # Remove stopped container
docker rm -f CONTAINER_ID # Force remove running container
📦 2. Image Management
🔍 List Images
docker images
docker image ls # Alternative syntax
🏗️ Build an Image
docker build -t my_app:1.0 .
-t
: Tags the image (format:name:tag
).
: Build context (Dockerfile location)
🗑️ Delete Images
docker rmi IMAGE_ID
docker image prune # Remove unused images
👀 3. Monitoring & Debugging
📊 List Containers
docker ps # Running containers
docker ps -a # All containers (including stopped)
📝 View Logs
docker logs CONTAINER_ID
docker logs -f CONTAINER_ID # Follow live logs (like tail -f)
🔍 Inspect Containers/Images
docker inspect CONTAINER_ID # Detailed JSON config
docker stats # Live resource usage (CPU/MEM)
🌐 4. Networking
🌉 Create a Network
docker network create my_network
🔗 Connect Containers
docker run --net=my_network --name app1 my_image
docker run --net=my_network --name app2 my_image
- Containers
app1
andapp2
can now communicate via hostnames!
� Port Forwarding
docker run -p 3306:3306 mysql # Host:Container port mapping
💾 5. Data Persistence (Volumes)
📂 Create a Volume
docker volume create my_volume
📌 Mount a Volume
docker run -v my_volume:/app/data my_image
- Persists data even after container deletion.
📋 List Volumes
docker volume ls
🛠️ Bonus: Pro Tips
-
Clean Up Everything 🧹:
docker system prune -a --volumes
- Removes stopped containers, unused networks, dangling images, and volumes.
-
Run Interactive Shell 💻:
docker exec -it CONTAINER_ID /bin/bash
- Debug containers by accessing their shell.
-
Update All Images 🔄:
docker images | awk 'NR>1 {print $1}' | xargs -L1 docker pull
🎉 Now you’re a Docker pro! Bookmark this cheat sheet and share it with your team. For more details, check the official Docker docs.
💬 Got questions? Drop them in the comments below! 👇