5 Docker Commands You Don't Know Yet
Tags: docker, devops, beginners
Published: 2023-08-10 23:31:46

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!

<img width="100%" style="width:100%" src="https://media.giphy.com/media/95ZYXmOCd9BBK/giphy.gif">


## 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`

![docker system df](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lf3hyctymn244hr8tgj3.png)


## 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!

![docker stats](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a2wii8889st0puwhlz7o.png)


## 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:

```bash
# 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:


![docker cp](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/01qd2906digcw00z6qio.png)

## 5. `docker top`

Alright, last but not least we have `docker top`! [`docker top`](https://docs.docker.com/engine/reference/commandline/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!


![docker top containerName](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kih5pbsj7cfphm4ay8fw.png)


## 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!](https://sliplane.io?utm_source=dockercommands)