Docker Community Forums

Share and learn in the Docker community.

How should I automate the log export process with volume in docker compose

I have 2 questions.

  1. I know that the docker container logs are stored in var/lib/docker/container folder, but I cannot find it with docker exec -it container_name bash
  2. How should I write the volume in docker compose to automatically export the container logs to host file system?

Thank you

It is best practice to let the application inside the container log to the console, as in STDOUT and STDERR. For containers created from images that follow that practice, you won’t be able to find the logs in the filesystem.

If logs are written into the containers filesystem, it can bloat your container - especialy if the application uses a debug or trace loglevel. To remedy this problem, you would need to trim the logs inside the container. This approach is not recommended.

Thank you for the explanation!
Is there a way to copy out logs without bloating the container?

Just to be sure: can you put what I wrote in my last response into own words and elaborate what you mean by your last question?

  1. I assume that the container created from image, the logs are written to the container filesystem(path: var/lib/docker/container), but I cannot find it. From your last post I guess my understanding was wrong. But where can I find the logs?
  2. Is there a way to save logs to a dedicated directory in the console?

There are multiple logging drivers. The default saves the log messages to a json file. You can find this file if you run

docker container inspect test --format '{{ .LogPath }}'

Usually you don’t need to work with it directly