9 Essential Docker Commands for Live Operations
This guide walks through the nine most frequently used Docker commands for online operations, showing how to list containers, view logs, exec into containers, monitor resource usage, inspect details, manage images, restart services, and clean up unused resources, with practical examples and troubleshooting scenarios.
1. View running containers
List currently running containers: docker ps Include stopped containers with the -a flag: docker ps -a Typical output:
CONTAINER ID IMAGE STATUS
a12b34cd nginx Up 3 days
b23c45de67 redis Up 5 days2. View container logs
Show logs for a specific container (e.g., nginx): docker logs nginx Common options: -f – follow logs in real time. --tail 100 – display the last 100 lines. --since 10m – show logs from the last 10 minutes.
3. Enter a container
Open an interactive shell inside a container: docker exec -it nginx bash If the image does not contain bash, fall back to sh: docker exec -it nginx sh Typical tasks inside the container include inspecting configuration files, checking processes, or testing network connectivity.
4. View container resource usage
Show live CPU, memory, and I/O statistics for all containers: docker stats Monitor a single container by specifying its name: docker stats nginx Sample output:
CONTAINER CPU % MEM USAGE
nginx 2.1% 120MB
redis 0.5% 80MB5. Inspect container details
Retrieve the full JSON description of a container: docker inspect nginx Common queries:
IP address: docker inspect nginx | grep IPAddress Mounted directories: inspect the Mounts field in the JSON output.
6. Restart containers
Restart one or multiple containers:
docker restart nginx
docker restart nginx redis mysql7. Stop and start containers
Gracefully stop a container: docker stop nginx Start it again: docker start nginx Force termination when a container does not stop cleanly:
docker kill nginx8. Manage images
List all images on the host: docker images Typical output shows repository, tag, and size:
REPOSITORY TAG SIZE
nginx latest 133MB
redis 7 98MB
mysql 8.0 447MBDelete an unused image by its ID:
docker rmi <image-id>9. Clean up unused resources
Check disk usage of Docker objects: docker system df Remove all stopped containers, unused networks, dangling images, and build cache: docker system prune Force removal without confirmation (including unused images): docker system prune -a Remove unused volumes:
docker volume pruneOperational cheat sheet
When a service is down:
docker ps
docker logs <container>When CPU spikes:
docker stats
docker exec -it <container> topWhen network issues appear:
docker inspect <container>
docker exec -it <container> ping …When configuration problems arise, open a shell and inspect files (e.g., /etc/nginx/nginx.conf).
When disk space is exhausted:
docker system df
docker system pruneSigned-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Architect Chen
Sharing over a decade of architecture experience from Baidu, Alibaba, and Tencent.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
