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! ๐๐