How to create log rotation for a container?

I have a container that has a file called “Container_ID.json.log” which is currently at 6G.
I know that it is not a good practice to simply rotate this log manually (Doing >/log/…json.log) since it can break the JSON Structure and generate some problems.

So, my question is: How can i rotate this log? Lets say, i don’t want it to pass 1G max, or how do i clear it the correct way?
Stopping the container and and bringing it back again is not practical for me.

I am sorry if this seems as a newbie question but the truth is i am a beginner at docker. Really have no idea on how to do this - And i do not want to do things not following best practices, or things that can create other problems.

Thank you

Have you tried to search for “docker log rotation”? :slight_smile:

This was my first result in an incognito window, so not because Google knew me.

You can find the “max-file” and “max-size” options here. The other way would be to send the logs to somewhere else and do log rotation there. For example you can send the logs to a remote syslog server or just send it to the local journal. The latter would still alow you to use docker logs but you would not have a json file. In that case you would need to configure journald properly. If you can’t do that, “max-file” and “max-size” with the json driver can be enough.