Home  >  Article  >  Operation and Maintenance  >  Docker log size limit and switching storage directory

Docker log size limit and switching storage directory

WBOY
WBOYforward
2023-06-09 19:18:421320browse

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.

Log Limitation

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"
}
}
  • max-size: The maximum size of a single log file;
  • max-file: A maximum of several log files are retained. When the log size of a single file exceeds the setting, a new log file will be generated.

3. Restart the Docker service to make the configuration take effect:

sudo systemctl restart docker

Data directory switching

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"
}
  • /home/docker The directory is external storage or a volume with relatively large space.

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/
  • A tool for remote synchronization of files and directories;
  • Tells rsync to synchronize files and directories in archive mode, where a represents archive mode, q represents quiet mode (no output is shown), x represents not crossing file system boundaries, and P .

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.

The difference between rsync and cp and rsync

  • Copy method: cp and scp will copy the entire file to the target location, while rsync will only copy the part that needs to be updated, which can improve Speed ​​and efficiency of replication.
  • Supportability: rsync supports more operations, such as file synchronization, file backup, file recovery, etc. cp and scp only support file copying.
  • Transmission method: cp copies files between local file systems, scp performs remote operations, and rsync can synchronize files between local or remote machines.
  • Efficiency: rsync is more efficient because it only copies files that need to be updated.
  • Options: rsync provides more options and configuration options, such as compression, partial transfer, cross-file system synchronization, etc.

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.

Summary

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!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete