Sending docker container logs to a separate file

In a large web application, the backend logs can get quite verbose: requests created, sent, processed, received, etc. The list can grow large very quickly depending on how logs are implemented.

You can check the logs from a Docker container using docker logs, no need to exec into the container:

docker logs <container_id>

You can find the <container_id> with docker ps and it’s not necessary to use the full ID. The first few characters is enough. So if the container id is e1215bf8014c, the command will look like:

docker logs e121

If you run docker logs you will probably see screens and more screens of logs. You can limit the logs to the last n lines with --tail:

docker logs --tail 500 <container_id>

The command above will only display the last 500 lines. Much better.

But reading logs on the terminal is no fun so you might also want to direct the logs to a file that can be opened in the text editor, for example. To output the docker container logs to a file, run:

docker logs --tail 500 <container_id> > mylogs.txt

The post Sending docker container logs to a separate file was originally published at flaviabastos.ca