Useful Docker Commands for the Self Hosting Enthusiast
Here's a few of my favorite Docker commands to use in a self hosted, homelab environment.
There's over 70 Docker commands and several other sub commands but you will most likely only use a handful of these as you tinker about with self hosting on your homelab.
Docker Stats
The docker stats
command will display a live stream of stats for each container that is running on the system. It's almost like an htop for Docker!
Docker System Prune
The docker system prune
command is essential for those with limited storage. This will remove items that are no longer in use. Items such as images, networks and cache that take up precious space on the system.
Docker PS
The docker ps
command will display all of the running containers on the system. Docker has used the naming convention of ps from Linux; ps means 'process status' in Linux, and containers are actually running as a process on the Linux server so that's why docker ps
is used to list the containers. You can also use docker ps -a
to list both running and stopped containers where -a means all.
SSH into Docker Containers
The docker exec -it containername /bin/bash
command allows you to SSH into a container. Here, you can execute pretty much any Linux command. For example, you can execute the top
command. This command shows information about the running system processes and performance. You may also encounter Docker images that require DB migrations that utilize this command.
Docker Logs
The docker logs containerID --tail 50 --follow
command will show you the logs of the container from the last 50 lines then continue to feed the logs live in the terminal.
The --follow
flag tells the command to coninue piping the logs live in the terminal.
Here are a few examples without the --follow
flag
Display the 100 most recent log lines
docker logs containerID --tail 100
Display log lines written in the past hour
docker logs containerID --since 1h
Display up to 100 log lines written in the past hour
docker logs containerID --tail 100 --since 1h
Docker RM
The docker rm
command is useful for removing containers when you need to update a container with a new image. For example, docker stop portainer
then docker rm portainer
will stop the Portainer container then remove it. Then I can install the latest version of Portainer on the system.
Final Notes and Thoughts
I highly recommend reading more about Docker commands to teach yourself more about how Docker works. You can see the complete CLI Docker command list on the Docker docs website here.
Let us know your favorite commands and how you use them in the comments below!