Notes on Docker Pitfalls¶
Info
Author: Void, published on December 9, 2021, reading time: about 6 minutes, WeChat official account article link:
1. Introduction¶
We often use Docker to run some services, but we only use a small part of its functionalities. Docker itself has many functions and commands. As a non-professional developer, I would like to record the pitfalls I have encountered in Docker in this article.
2. Pitfalls¶
2.1 Starting Containers¶
Docker containers run in the background and must have a persistent foreground process. Otherwise, the container will automatically exit.
A common practice is to use CMD or ENTRYPOINT in the Dockerfile to declare the command we want to run, or to declare the command after Docker run. However, sometimes our command may not be easy to write here.
In this case, we can start the container as a daemon process.
docker run -itd image id
With the following command, we can enter the container.
docker exec -it container id /bin/bash
In the container, we can freely execute our commands.
Finally, we use Ctrl+P+Q to exit the container without closing it.
2.2 Text Editor¶
The native Linux environment may not have a text editor. We can use cat > file to paste the content into the text, but this method is not very user-friendly.
Vim is a commonly used text editor under Linux. We can install Vim with the following command.
apt-get update
apt-get install vim
2.3 Checking Container Status¶
Check the processes running in the container.
docker top container id
For unwanted processes, we can use the following command to kill them.
kill -9 PID
View the log of processes running in the container.
docker logs -f -t --tail 20 container id
2.4 Cleaning Up Unused Volumes¶
We often use Docker to create and delete containers. Over time, the data volumes mounted by the container can produce some zombie files (unbound containers, etc.).
We can use the following command to find zombie files.
docker volume ls -qf dangling=true
And delete these zombie files.
docker volume rm $(docker volume ls -qf dangling=true)
3. Conclusion¶
These are just the pitfalls and knowledge the author has learned while using Docker, and are not comprehensive. There is still much room for exploration in Docker.
I hope the readers can avoid some pitfalls and bugs.