Docker CLI Tricks Every Developer Should Know

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 Cont…


This content originally appeared on DEV Community and was authored by Suleiman Dibirov

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 with docker 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 use docker image prune, docker container prune, and docker 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, and docker-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.


This content originally appeared on DEV Community and was authored by Suleiman Dibirov


Print Share Comment Cite Upload Translate Updates
APA

Suleiman Dibirov | Sciencx (2024-11-02T06:30:00+00:00) Docker CLI Tricks Every Developer Should Know. Retrieved from https://www.scien.cx/2024/11/02/docker-cli-tricks-every-developer-should-know/

MLA
" » Docker CLI Tricks Every Developer Should Know." Suleiman Dibirov | Sciencx - Saturday November 2, 2024, https://www.scien.cx/2024/11/02/docker-cli-tricks-every-developer-should-know/
HARVARD
Suleiman Dibirov | Sciencx Saturday November 2, 2024 » Docker CLI Tricks Every Developer Should Know., viewed ,<https://www.scien.cx/2024/11/02/docker-cli-tricks-every-developer-should-know/>
VANCOUVER
Suleiman Dibirov | Sciencx - » Docker CLI Tricks Every Developer Should Know. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/11/02/docker-cli-tricks-every-developer-should-know/
CHICAGO
" » Docker CLI Tricks Every Developer Should Know." Suleiman Dibirov | Sciencx - Accessed . https://www.scien.cx/2024/11/02/docker-cli-tricks-every-developer-should-know/
IEEE
" » Docker CLI Tricks Every Developer Should Know." Suleiman Dibirov | Sciencx [Online]. Available: https://www.scien.cx/2024/11/02/docker-cli-tricks-every-developer-should-know/. [Accessed: ]
rf:citation
» Docker CLI Tricks Every Developer Should Know | Suleiman Dibirov | Sciencx | https://www.scien.cx/2024/11/02/docker-cli-tricks-every-developer-should-know/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.