D: Docker has revolutionized the way we develop, ship, and run applications. Whether you’re a beginner or a seasoned developer, mastering Docker commands is essential. This guide covers everything from basic to advanced Docker commands with practical examples. Letβs dive in!
1. Getting Started with Docker Basics
πΉ docker --version
Check your Docker installation and version.
docker --version
# Output: Docker version 20.10.17, build 100c701
πΉ docker run
– Run a Container
The most fundamental command. Pulls an image (if not locally available) and starts a container.
docker run hello-world # Runs the official "hello-world" image
πΉ docker ps
– List Running Containers
docker ps # Shows active containers
docker ps -a # Shows all containers (including stopped ones)
πΉ docker stop
& docker start
– Manage Containers
docker stop
<container_id> # Gracefully stops a container
docker start
<container_id> # Restarts a stopped container
2. Working with Docker Images
πΉ docker images
– List Downloaded Images
docker images # Lists all locally stored images
πΉ docker pull
– Download an Image
docker pull nginx:latest # Downloads the latest Nginx image
πΉ docker rmi
– Remove an Image
docker rmi nginx # Deletes the Nginx image (if no containers are using it)
πΉ docker build
– Create an Image from a Dockerfile
docker build -t my-custom-image . # Builds an image from Dockerfile in current dir
3. Advanced Container Management
πΉ docker exec
– Run Commands Inside a Running Container
docker exec -it
<container_id> bash # Opens an interactive shell
πΉ docker logs
– View Container Logs
docker logs
<container_id> # Shows logs (useful for debugging)
πΉ docker inspect
– Get Detailed Container Info
docker inspect
<container_id> # Returns JSON metadata (IP, volumes, etc.)
4. Networking & Port Forwarding
πΉ docker network ls
– List Networks
docker network ls # Shows available Docker networks
πΉ docker run -p
– Port Mapping
docker run -p 8080:80 nginx # Maps host port 8080 → container port 80
πΉ docker network create
– Create Custom Networks
docker network create my-network # Creates a new bridge network
5. Volume Management (Persistent Data)
πΉ docker volume ls
– List Volumes
docker volume ls # Shows all Docker volumes
πΉ docker run -v
– Mount a Volume
docker run -v /host/path:/container/path nginx # Binds a host directory
πΉ docker volume create
– Create a Named Volume
docker volume create my-data # Creates a persistent volume
6. Docker Compose for Multi-Container Apps
πΉ docker-compose up
– Start Services
docker-compose up -d # Runs containers in detached mode
πΉ docker-compose down
– Stop Services
docker-compose down # Stops and removes containers, networks
πΉ docker-compose logs
– View Logs
docker-compose logs -f # Follow logs in real-time
7. Cleaning Up Docker
πΉ docker system prune
– Free Up Space
docker system prune -a # Removes unused images, networks, and containers
πΉ docker rm
– Remove Stopped Containers
docker rm $(docker ps -aq) # Deletes all stopped containers
π Pro Tips for Docker Power Users
- Use
--restart unless-stopped
to auto-restart containers. - Combine
docker stats
for real-time resource monitoring. - Leverage
.dockerignore
to speed up builds (like.gitignore
).
Final Thoughts
Mastering Docker commands unlocks the full potential of containerization. From simple docker run
to complex docker-compose
setups, these commands form the backbone of modern DevOps.
π‘ Now it’s your turn! Try these commands in a sandbox environment and experiment. Happy Dockerizing! π³
π Further Learning:
Got questions? Drop them in the comments! ππ