D: 🚢 Docker is a game-changer for developers, but its command-line interface can be intimidating for beginners. Don’t worry! This guide breaks down essential Docker commands with clear examples and practical tips. Let’s dive in!
🔹 1. Getting Started with Docker
Installation & Version Check
Before using Docker, ensure it’s installed:
docker --version # Check Docker version (e.g., "Docker version 24.0.7")
Hello World Test
Run your first container:
docker run hello-world # Downloads & runs a test image
✅ Output: A welcome message confirms Docker is working!
🔹 2. Core Docker Commands
📦 Working with Images
- Pull an image (download from Docker Hub):
docker pull nginx # Downloads the latest Nginx image
- List images:
docker images # Shows all downloaded images
- Remove an image:
docker rmi nginx # Deletes the Nginx image
🐳 Managing Containers
- Run a container:
docker run -d -p 8080:80 --name my-nginx nginx # -d: Detached mode (runs in background) # -p: Maps host port 8080 → container port 80
- List running containers:
docker ps # Shows active containers docker ps -a # Shows ALL containers (including stopped ones)
- Stop/Start/Remove a container:
docker stop my-nginx # Stops the container docker start my-nginx # Restarts it docker rm my-nginx # Deletes the container (must be stopped first)
🔹 3. Handy Tips & Tricks
🔍 Inspecting Containers
- View logs:
docker logs my-nginx # Shows container logs
- Enter a running container (like SSH):
docker exec -it my-nginx bash # Opens a shell inside the container
🧹 Cleanup
- Remove all stopped containers:
docker container prune
- Delete unused images:
docker image prune -a
🔹 4. Pro Tips for Beginners
- Use
--help
: Append to any command for docs (e.g.,docker run --help
). - Tag images wisely: Always specify versions (e.g.,
nginx:1.25
). - Persist data: Use volumes (
-v /host/path:/container/path
) to save data.
🎉 Final Thoughts
Mastering these commands unlocks Docker’s potential! Practice with small projects (e.g., running a Python app in a container) to build confidence.
💬 Got questions? Drop a comment below! 👇
#Docker #DevOps #Containers #Programming