Docker is a key tool in modern development infrastructures, essential for managing containerized applications. In this guide, we detail the 30 must-know Docker commands for developers and DevOps engineers, broken down by category. All examples reflect real-world use cases!
—.
π οΈ 1. basic commands & info check docker version?
docker version
Check Docker client and server version
docker version
# Client: Docker Engine - Community
# Server: Docker Engine - Community
```bash docker version
#### **`docker info`**
Get general information about your Docker system (number of containers/images, memory usage, etc.)
```bash
docker info | grep Containers
# Containers: 14
```bash docker info | grep Containers # Containers: 14
---]
### π¦ **2. Manage images** **Docker images
#### **`docker images`**
Output a list of locally stored docker images
```bash
docker images
# repository tag image id created size
# nginx latest 605c77e624dd 2 months ago 141MB
docker pull
Download the image from the registry
docker pull ubuntu:22.04
docker rmi
Delete the local image
docker rmi nginx:alpine
docker build
Build an image with a Dockerfile
docker build -t my-app:1.0 .
# -t: tagging, ..: Use Dockerfile in current directory
```bash docker build -t my-app:0 .
#### **`docker tag`**
Give the image a new tag
``` bash
docker tag my-app:1.0 my-registry.com/my-app:prod
docker push
Upload the image to the registry
docker push my-registry.com/my-app:prod
docker commit
Create a new image from the running container
docker commit my-container my-snapshot
—]
π 3. Run & Manage Containers *!
docker run
Run a new container (βοΈ most essential!)
docker run -d -p 8080:80 --name web nginx
# -d: run in the background, -p: port mapping, --name: specify a name
docker ps
Check the list of running containers
docker ps -a # -a: get full list including stopped containers
docker stop
Stop a running container
docker stop web
```bash
#### **`docker start`**
Restart a stopped container
```bash
docker start web
```bash
#### **`docker rm`**
Delete a container
```bash
docker rm web
docker rm $(docker ps -aq) # Delete all containers
docker exec
Run a command inside a running container
docker exec -it web bash
# -it: interactive terminal mode
docker cp
Copy files between hosts β containers
docker cp index.html web:/usr/share/nginx/html
—]
π 4. Monitoring & Logs *!
docker logs
Check container logs
docker logs -f web # -f: live streaming
docker stats
Monitor real-time resource usage
docker stats
# container id name cpu % mem usage / limit
docker top
Check the running processes inside the container
docker top web
```bash
#### **`docker inspect`**
Output container/image details JSON
```bash
docker inspect web | grep IPAddress
—]
π 5. networking **!
docker network ls
Get a list of networks
docker network ls
# network id name driver
docker network create
Create a custom network
docker network create my-net
docker network connect
Connect a container to the network
docker network connect my-net web
—]
πΎ 6. Managing Volumes **Create a Volume
docker volume create
Create a volume for persistent data storage
docker volume create db-data
docker volume ls
List the volumes
docker volume ls
docker volume inspect
Check volume details
docker volume inspect db-data
—]
π 7. Docker Compose
docker-compose up
Run the docker-compose.yml-based service
docker-compose up -d # Run in the background
```bash docker-compose up
#### **`docker-compose down`**
Stop and delete a service run by Compose
``` bash
docker-compose down --volumes # Delete volumes together
```bash docker-compose down --volumes
---]
### π§Ή **8. Manage the system** **Prune the system
#### **`docker system prune`**
Batch clean up unused resources
```bash
docker system prune -a --volumes
# -a: --volumes, including unused images: Delete volumes
```bash docker system prune
#### **`docker login`**
Registry login (Docker Hub, etc.)
```bash
docker login registry.example.com
```bash docker login
#### **`docker logout`**
Log out of the registry
```bash
docker logout registry.example.com
docker search
Search for images in Docker Hub
docker search --limit 5 redis
```bash docker search --limit 5 redis
---]
### π **Finishing touches
By learning these 30 commands, you'll be able to perform over 90% of the tasks in the Docker ecosystem! These are the commands we use most often in real-world service operations. π―
> **Practical Tips**
> - Set automatic container restart with `--restart=always` when running `docker run`.
> - Avoid using `docker attach` instead of `docker exec -it` (it will kill the process when the container is terminated)
> - Always use `docker-compose` in production
The best way to learn is by typing commands. Happy Dockerizing! π³π»