Will the output of programs in docker (such as’ print’) be saved to disk?

  docker, question

Problem description

After running for several days, the container in the server was full of disks. after searching for large files, it was found that/var/lib/dockerUnder pathcontainerThere is a in the folderlogThe file at the end is very large. After deleting, start the program and check the directory after half an hour:

The environmental background of the problem and what methods have you tried?

At that time, the temporary method was to write a script to delete the log file regularly.

Only print output is included in the code, and the log file is not saved.

After modifying the code to remove the printout, the log file did not appear again. I suspect docker automatically collected these prints.

In the end, I’m still not sure if it was becauseDocker automatically collects these printsThe server disk is full. Please give your predecessors some advice.

After consulting the data, we know that:

  • The above logs all belong to standard output (stdout), python’s print golang’s fmt.Println all belong to stdout;
  • The logging mechanism in docker is called log driver.

    • Each container is a special process that is created and started by the docker daemon, guarded and managed by the docker daemon, so the docker daemon can obtain the stdout; of the container;
  • Docker daemon has a default log driver, which defaults to json-file; ;

    • Json-file will write the standard output and standard errors of all containers into a file in json format. This file records one standard output or sta ndard error per line and annotates it with time stamps.
  • Change the default log launcher, as follows:

    • Edit docker deamon profile:sudo vim /etc/docker/daemon.json
    • Add a new article:{"log-driver": "none"}(You can also add{"log-opts": {"max-size": "10m" }}To control the size of the log file)
    • Reload the configuration file and restart the docker service:sudo systemctl daemon-reloadsudo systemctl restart docker
    • After modifying the configuration, restart the identification task without generating log file.

Information reference address:
Log for Docker Production Environment-JSON File Log Driver
Configure log drivers