Docker has become a must-have skill for developers and DevOps engineers. Whether you're deploying microservices, testing in isolated environments, or optimizing workflows, Docker makes it all easier.
But to truly leverage Dockerโs power, you need to master its command-line interface (CLI).
This guide walks you through the most essential Docker commands, complete with usage details and real-world examples.
๐งฑ 1. Image Management Commands
Docker images are the blueprints for containers. These commands help you manage and manipulate them efficiently.
๐ณ docker pull
Download an image from a registry (like Docker Hub).
Usage
docker pull [OPTIONS] NAME[:TAG|@DIGEST]
Example
docker pull ubuntu:latest
๐ฆ docker images
(or docker image ls
)
List all local Docker images.
docker images
๐จ docker build
Build an image from a Dockerfile.
docker build -t my-app:1.0 .
๐๏ธ docker rmi
Remove one or more images.
docker rmi my-app:1.0
๐ docker push
Push an image to a registry.
docker push myusername/my-app:1.0
๐งฉ 2. Container Management Commands
Containers are runnable instances of Docker images. Manage their lifecycle with these commands.
โถ๏ธ docker run
Run a container from an image.
docker run -d -p 8080:80 --name my-nginx nginx
๐งญ docker ps
List running containers.
docker ps
To include stopped containers:
docker ps -a
๐ docker start
/ docker stop
/ docker restart
Manage container state:
docker start my-nginx
docker stop my-nginx
docker restart my-nginx
๐งน docker rm
Remove containers.
docker rm my-nginx
# Force remove
docker rm -f my-nginx
๐ฌ docker exec
Execute commands inside a running container.
docker exec -it my-nginx bash
๐ docker logs
View container logs.
docker logs my-nginx
# Follow logs live
docker logs -f my-nginx
๐ 3. Network Management Commands
Docker networks enable container communication.
๐ docker network ls
List networks.
docker network ls
๐งฑ docker network create
Create a custom network.
docker network create my-custom-network
๐ docker network connect
/ disconnect
Manage container network connections.
docker network connect my-custom-network my-nginx
docker network disconnect my-custom-network my-nginx
โ docker network rm
Remove a network.
docker network rm my-custom-network
๐พ 4. Volume Management Commands
Docker volumes persist data between container restarts or removals.
๐ docker volume ls
List all volumes.
docker volume ls
โ docker volume create
Create a named volume.
docker volume create my-data
๐ docker volume inspect
Inspect a volume.
docker volume inspect my-data
๐งจ docker volume rm
Remove a volume.
docker volume rm my-data
โ๏ธ 5. System-Wide Commands
These commands give you visibility into Dockerโs overall state and allow you to keep things tidy.
๐ง docker info
View Docker system information.
docker info
๐งพ docker version
Check Docker version details.
docker version
๐งผ docker system prune
Clean up unused containers, images, and networks.
docker system prune -a
๐ Conclusion
By mastering these Docker commands, youโll be able to:
- Manage containers and images efficiently
- Simplify networking and data persistence
- Keep your Docker environment clean and optimized
๐ก Pro tip: Experiment with Docker Compose next โ it lets you manage multi-container applications effortlessly.
๐ฌ Whatโs your most-used Docker command? Drop it in the comments below!
If you found this guide helpful, consider following me for more DevOps & Cloud tips ๐
Top comments (0)