You just started with your Docker Journey and think you know everything? Strap in and learn some fancy new commands to impress your friends and boss with!
1. docker system df
If you're anything like me, you're probably building docker containers left and right without really thinking about where everything is stored. Want to find out how much storage your containers, images, volumes, and cache artifacts need? Simply run docker system df
and be amazed at how much of your disk is just used for docker 🤓
PS: Too much space used? Try this: docker system prune --all
2. docker stats
Continuing with monitoring commands, did you know that you can see exactly how much resources each container is using? You can see the CPU, memory, and even network usage! Especially useful if you're running containers you haven't built yourself and you need to keep a close eye on them 👀
Simply run docker stats
to see all the juicy details!
3. docker build --cpu-quota=50000
Everyone knows that Docker likes to use all of the available CPU resources, making everything else SUPER slow during the build process. But what if we could tell Docker how much of your available CPU to use? 🤔
The solution to this problem is the --cpu-quota
flag of the docker build
command which isn't even documented when executing docker build --help
! The --cpu-quota
flag allows you to define how much of your CPU cores can be used. Every core equals 100000. If you have a 4 core CPU and you want docker build
to use a maximum of 80% of your CPU, you would need to calculate 4 * 100000 * 0.8 = 320000, and then set --cpu-quota 320000
. On Linux or Macos you can also calculate it automatically:
# Linux
docker build --cpu-quota $(( $(grep -c ^processor /proc/cpuinfo) * 100000 * 8 / 10 )) -t your_image_name your_docker_directory
# MacOS
docker build --cpu-quota $(( $(sysctl -n hw.logicalcpu) * 100000 * 8 / 10 )) -t your_image_name your_docker_directory
4. docker cp
The docker cp command is useful for copying files or directories between a running Docker container and your local filesystem. This is especially handy when you need to extract logs or output files from a container. The command is quite simple: You need your containers name, the path of the file inside your container, and the path on your filesystem where the file should be copied to. Then combine it to get something like that:
5. docker top
Alright, last but not least we have docker top
! docker top
prints out the currently running processes inside a running container. If you start a container with --name myapp
, simply run docker top myapp
and you will see what is going on inside the container. This is especially great if you're debugging a container and you're not sure if your CMD
command is correct!
Conclusion
I really hope you learned one or two new docker commands today! If you need any help, or have any questions about these commands, please write a comment! And if you want to deploy your own Dockerized Apps, check out Sliplane!
Top comments (14)
The --cpu-quota flag is indeed quite valuable. Appreciate you sharing this!
Here are links to the reference documentation:
Thanks! Completely forgot to add them:)
Thanks for sharing this informative post on Docker commands. I found it very helpful and easy to understand. The way you explained the commands with examples made it easier for me to grasp the concepts.
Great tips! Another good one is to use lazydocker github.com/jesseduffield/lazydocker
Ohh that looks nice, didn't know it before. Going to check it out!
that was interesting
Interesting.
Glad that you found it interesting:)
Really cool!
The "docker build --cpu-quota" didn't know their existence; I agree with you; docker ate all the CPU, and you must go for a coffee in the nearby coffee shop.
Really Cool!
glad you enjoyed it :)
Wow, build cache can really bite into your disk space, never took the time to inspect. Thanks! 🙌🏻