Cloud Native 7 min read

10 Essential Docker CLI Tricks to Boost Your Productivity

This guide presents ten powerful Docker CLI commands—from extracting container details with docker inspect to cleaning unused resources, limiting resource usage, viewing logs, exporting images, monitoring stats, dynamic port binding, and using docker‑compose—each accompanied by practical tips that streamline container management and improve developer efficiency.

Code Mala Tang
Code Mala Tang
Code Mala Tang
10 Essential Docker CLI Tricks to Boost Your Productivity

Docker CLI provides powerful commands that can significantly boost productivity, simplify workflows, and make container management more efficient. Below are ten essential tips and tricks every developer should know.

1. Use docker inspect to extract container information

<code># New Docker client syntax
docker inspect --format '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}'

# Old Docker client syntax
docker inspect --format '{{.NetworkSettings.IPAddress}}' &lt;container_name&gt;</code>

Tip: Use docker inspect to access detailed information about containers, images, and volumes, including IP addresses and mounted volumes. Combine with --format to extract specific fields.

2. Quickly clean up unused resources

<code>docker system prune</code>

Output example:

<code>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]</code>

Tip: Over time, unused containers, images, and volumes accumulate and consume disk space. docker system prune removes all unused content, or use docker image prune , docker container prune , and docker volume prune for selective cleanup.

3. Run one‑off commands inside a container

Tip: Use docker exec to execute a one‑off command in a running container without starting a new one. Example:

<code>docker exec -it my_container /bin/bash</code>

Useful for debugging and inspecting runtime configuration.

4. Limit container resource usage

<code># Command
docker run --memory <memory_limit> --cpus <cpu_limit>

# Example
docker run --memory=512m --cpus=1 my_image</code>

Tip: Restrict each container's memory and CPU usage to prevent excessive resource consumption and ensure fair allocation.

5. View container logs in real time

Tip: The -f option streams live log output, useful for debugging running services. Example:

<code>docker logs -f my_container</code>

You can also add --tail to show only recent lines:

<code>docker logs -f --tail 50 my_container</code>

6. Export and import images

<code># Export
docker save -o <filename.tar> <image_name>

# Import
docker load -i <filename.tar></code>

Tip: Export and import images between machines without re‑downloading, ideal for offline setups or limited network environments.

7. View running container statistics

<code>docker stats</code>

Tip: Use docker stats to monitor real‑time CPU, memory, and network usage, helping diagnose performance issues and visualize resource utilization.

8. Dynamically bind ports

<code>docker run -P &lt;image_name&gt;</code>

Tip: The -P flag maps exposed container ports to random available host ports. View the mapping with:

<code>docker port &lt;container_name&gt;</code>

Alternatively, use -p to specify a host port, e.g., docker run -p 8080:80 my_image .

9. Use docker-compose to quickly build and run containers

<code>docker-compose up -d</code>

Tip: For multi‑container applications, docker-compose simplifies setup. The -d flag runs services in the background, while docker-compose up without -d recreates only modified containers, speeding up development.

10. Use docker system df to view disk usage

<code>docker system df</code>

Output example:

<code>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</code>

Tip: Check how much disk space Docker resources (images, containers, volumes) occupy. This command provides an overview of storage usage, aiding resource management.

Conclusion

Mastering these Docker CLI tricks can significantly improve your workflow, from efficient resource management to rapid application debugging.

CLIDockerdevopsproductivityContainer Management
Code Mala Tang
Written by

Code Mala Tang

Read source code together, write articles together, and enjoy spicy hot pot together.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.