Home > Article > Operation and Maintenance > Docker log size limit and switching storage directory
Various environments of the product use a lot of CentOS virtual machines. By default, the root directory space is 50 G. As the use time becomes longer and longer, the space will become insufficient. .
The method that has been used is to clear useless images and Docker logs, so that the server will always be available. It wasn't until cleaning was useless that I thought about finding other methods.
When we are not forced into a desperate situation, we will habitually rely on methods and tools that we are familiar with. It may not be efficient, but it can solve the problem. This kind of thinking is not unusable and is harmful to people. People stay in their comfort zone and don't want to come out.
The other methods mentioned above are actually very simple and can be obtained by checking the information. They are to limit the Docker log size and switch the Docker data directory to external storage.
1. Edit the Docker configuration file /etc/docker/daemon.json. If the file does not exist, create a new one:
sudo vi /etc/docker/daemon.json
2. In Add the following content to the file to limit the size of a single log file to 100MB and retain the most recent 5 log files. Here we use the max-size and max-file parameters to control the size and number of logs:
{ "log-driver": "json-file", "log-opts": { "max-size": "100m", "max-file": "5" } }
3. Restart the Docker service to make the configuration take effect:
sudo systemctl restart docker
In CentOS, the default directory of Docker is /var/lib/docker , you can use the data-root option in the Docker configuration file to set the Dcoker data directory. The specific steps are as follows:
1. Add the data-root option in the /etc/docker/daemon.json configuration file:
{ "log-driver": "json-file", "log-opts": { "max-size": "100m", "max-file": "5" } "data-root": "/home/docker" }
2. Disable Docker.
sudo systemctl stop docker
3. Copy the contents of the Docker default directory to a new directory:
sudo rsync -aqxP /var/lib/docker/ /home/docker/
4. Modify the name of the default directory to bak:
mv /var/lib/docker /var/lib/docker.bak
The advantage of this is that you can back up the original data and delete it after the operation is stable. In addition, it prevents configuration If it does not take effect, the original directory will still be read.
5. Enable Dcoker:
sudo systemctl start docker
In the third step above, the rsync command was used to synchronize content. The meaning of this command is to use the rsync tool to copy /var/ on the local computer. All files and subdirectories in the lib/docker/ directory are synchronized to /home/docker/ in another local computer
Before this, cp and scp were mostly used to operate files or directories. This time, check I learned about the rsync command tool when reading the information, and then continued to learn about cp and scp.
In short, rsync is a more powerful and efficient file copy and synchronization tool. If you need to perform file synchronization, backup and recovery operations between local or remote machines, it is recommended to use rsync. And cp and scp are suitable for simple local file copying and remote file transfer.
Through this study of log restrictions and directory switching, I have two thoughts:
1. Many times, a better way is not far from you. It all depends on whether you are willing to take a step forward and explore it. That is to say, you can’t make do with it, and don’t have the mindset of “it’s not unusable.” It’s the same for making products and learning skills.
2. Many skills learned after work are learned through continuous problem solving. This will gradually make you more experienced, but not systematic. Even if you feel very familiar with a certain field, I think It is also necessary to read books and study systematically, which will definitely eliminate many blind spots.
The above is the detailed content of Docker log size limit and switching storage directory. For more information, please follow other related articles on the PHP Chinese website!