The Docker CLI offers powerful commands that can significantly improve productivity, simplify workflows, and make managing containers more efficient. Here are some essential tricks and tips that every developer should know.
1. Inspecting Containers with docker inspect
#modern Docker client syntax
docker inspect --format='{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
#old Docker client syntax
docker inspect --format='{{.NetworkSettings.IPAddress}}' <container_name>
-
Tip: Use
docker inspect
to access detailed information about containers, images, and volumes, including IP addresses and mounted volumes. Use--format
withdocker inspect
to extract specific fields:
2. Cleaning Up Unused Resources Quickly
docker system prune
#output example
WARNING! This will remove:
- all stopped containers
- all networks not used by at least one container
- all dangling images
- all dangling build cache
Are you sure you want to continue? [y/N]
-
Tip: Over time, unused containers, images, and volumes accumulate and take up disk space.
docker system prune
removes everything unused, or usedocker image prune
,docker container prune
, anddocker volume prune
for selective cleanup.
3. Running One-Off Commands in Containers
-
Tip: Execute one-off commands within a running container without starting a new one, using
docker exec
. For example:
docker exec -it my_container /bin/bash
Useful for debugging and checking runtime configurations inside containers.
4. Limiting Resource Usage on Containers
#command
docker run --memory <memory_limit> --cpus <cpu_limit>
#example
docker run --memory=512m --cpus=1 my_image
- Tip: Prevent resource hogging by limiting memory and CPU usage for each container. This ensures fair resource allocation:
5. Checking Container Logs in Real-Time
-
Tip: The
-f
option shows real-time log output, useful for debugging running services. For instance:
docker logs -f my_container
-
You can also add
--tail
to view only the most recent lines:
docker logs -f --tail 50 my_container
6. Exporting and Importing Images
#export
docker save -o <filename.tar> <image_name>
#import
docker load -i <filename.tar>
- Tip: Export and import images between machines without re-downloading them. Ideal for offline setups or environments with network restrictions.
7. Checking Running Container Stats
docker stats
-
Tip: Use
docker stats
to monitor real-time metrics for CPU, memory, and network usage. It helps diagnose performance issues and visualize resource utilization for each container.
8. Binding Ports Dynamically
docker run -P <image_name>
-
Tip: With
-P
, Docker maps exposed container ports to random available host ports. To see mapped ports, use:
docker port <container_name>
Alternatively, specify specific host ports using
-p
(e.g.,docker run -p 8080:80 my_image
).
9. Quickly Build and Run Containers with docker-compose
docker-compose up -d
-
Tip: For multi-container applications,
docker-compose
simplifies setup. The-d
flag runs services in the background, anddocker-compose up
recreates only modified containers, speeding up development.
10. Viewing Disk Usage with docker system df
docker system df
#output example
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 11 6 2.866GB 1.171GB (40%)
Containers 17 13 3.4MB 2.145MB (63%)
Local Volumes 9 5 849.5MB 315.1MB (37%)
Build Cache 0 0 0B 0B
- Tip: See how much disk space Docker resources (images, containers, volumes) occupy. This command provides an overview of storage use, which is helpful for resource management.
Conclusion
Mastering these Docker CLI tricks can make a noticeable difference in your workflow, from managing resources efficiently to quickly debugging applications.