D: 🚀 Docker has revolutionized the way developers build, ship, and run applications. But with so many commands and options, it’s easy to get overwhelmed. Don’t worry! This guide covers must-know Docker commands and pro tips to supercharge your workflow.
🔥 1. Essential Docker Commands
1️⃣ Container Management
✔ Run a Container
docker run -d -p 8080:80 --name my_container nginx
-d
: Run in detached mode (background)-p 8080:80
: Map host port 8080 to container port 80--name
: Assign a custom name
✔ List Running Containers
docker ps
- Add
-a
to see all containers (including stopped ones).
✔ Start/Stop a Container
docker start my_container
docker stop my_container
docker restart my_container
✔ Remove a Container
docker rm my_container
- Use
-f
to force remove a running container.
2️⃣ Image Management
✔ Pull an Image
docker pull ubuntu:latest
- Always specify a tag (
:latest
,:20.04
, etc.).
✔ List Images
docker images
✔ Remove an Image
docker rmi ubuntu
- Use
-f
if the image is in use.
✔ Build an Image from Dockerfile
docker build -t my_app:1.0 .
-t
: Tag the image (name:version
).
3️⃣ Networking
✔ List Networks
docker network ls
✔ Create a Custom Network
docker network create my_network
✔ Connect a Container to a Network
docker network connect my_network my_container
🚀 2. Pro Tips for Docker Power Users
💡 Tip 1: Use Docker Compose for Multi-Container Apps
Instead of running containers manually, define them in a docker-compose.yml
file:
version: '3'
services:
web:
image: nginx
ports:
- "8080:80"
db:
image: postgres
environment:
POSTGRES_PASSWORD: mypassword
Then run:
docker-compose up -d
💡 Tip 2: Clean Up Unused Resources
Free up disk space with:
docker system prune -a
- Removes stopped containers, unused networks, dangling images, and build cache.
💡 Tip 3: Debugging Containers
✔ View Logs
docker logs my_container
✔ Enter a Running Container
docker exec -it my_container bash
-it
: Interactive terminal mode.
💡 Tip 4: Use Volumes for Persistent Data
docker run -v /host/path:/container/path my_image
- Ensures data survives container restarts.
🎯 3. Real-World Use Cases
Case 1: Deploying a Web App
docker run -d -p 80:80 --name my_web_app nginx
- Instantly serve a static website.
Case 2: Running a Database
docker run -d --name mysql_db -e MYSQL_ROOT_PASSWORD=secret mysql
- Spin up a MySQL instance in seconds.
Case 3: CI/CD Automation
docker build -t my_app . && docker push my_app
- Build and push to Docker Hub for deployment.
✅ Final Thoughts
Docker is powerful but simple once you master the basics. Bookmark this cheatsheet 🚀, and you’ll save hours of debugging!
🔗 Want more? Check out Docker’s official docs: https://docs.docker.com
💬 Which Docker command do you use most often? Let us know in the comments! 👇