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!
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
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 👀
docker stats to see all the juicy details!
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
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:
Alright, last but not least we have
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!
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!