D: 🚀 Docker has revolutionized the way developers build, ship, and run applications. Whether you’re a beginner or an advanced user, mastering Docker commands is crucial for efficient container management. This guide covers everything from basic CLI commands to advanced techniques with practical examples.
🔹 1. Essential Docker Commands
These are the must-know commands to get started with Docker.
1.1. Container Management
-
docker run
: Creates and starts a container from an image.docker run -d -p 8080:80 --name my_nginx nginx
-d
: Detached mode (runs in background)-p 8080:80
: Maps host port 8080 to container port 80--name
: Assigns a custom name
-
docker ps
: Lists running containers.docker ps -a # Shows all containers (including stopped ones)
-
docker stop
/docker start
: Stops or starts a container.docker stop my_nginx docker start my_nginx
-
docker rm
: Removes a container.docker rm my_nginx # Removes a stopped container docker rm -f my_nginx # Force removes a running container
1.2. Image Management
-
docker pull
: Downloads an image from Docker Hub.docker pull ubuntu:latest
-
docker images
: Lists downloaded images.docker images
-
docker rmi
: Deletes an image.docker rmi nginx
🔹 2. Intermediate Docker Commands
Once you’re comfortable with basics, try these powerful commands.
2.1. Logs & Debugging
-
docker logs
: Views container logs.docker logs my_nginx docker logs -f my_nginx # Follow logs in real-time
-
docker exec
: Runs a command inside a running container.docker exec -it my_nginx bash # Opens an interactive shell
2.2. Networking
-
docker network
: Manages container networks.docker network ls # Lists networks docker network create my_network # Creates a custom network
-
docker inspect
: Displays detailed container info.docker inspect my_nginx
🔹 3. Advanced Docker Commands
For power users, these commands unlock Docker’s full potential.
3.1. Docker Compose (Multi-Container Apps)
-
docker-compose up
: Starts services defined indocker-compose.yml
.docker-compose up -d # Runs in detached mode
-
docker-compose down
: Stops and removes containers.docker-compose down --volumes # Also removes volumes
3.2. Volume Management
docker volume
: Manages persistent storage.docker volume create my_volume docker volume ls
3.3. Docker Build (Custom Images)
docker build
: Creates an image from aDockerfile
.docker build -t my_custom_image:1.0 .
🔹 4. Pro Tips & Best Practices
✅ Clean unused resources
docker system prune -a # Removes unused images, containers, networks
✅ Use .dockerignore
(Like .gitignore
but for Docker builds)
✅ Optimize Dockerfile
(Multi-stage builds, minimal base images)
🎯 Final Thoughts
Mastering these Docker commands will boost your productivity and help you manage containers like a pro! 🚀
💡 Want more? Check out the official Docker CLI docs for deeper insights.
Happy Dockering! 🐳