
1. Overview
In this article, we will learn to dump Docker logs into a file. You can learn more about Docker by following these articles.
2. Docker logs
You could have used docker logs
to get the logs of the container.
> docker logs CONTAINER_ID logs statements >
Most of the time, we would want to capture docker logs in a file and debug.
3. Docker logs dump to file
3.1. Default docker log file
Docker by default stores logs in a log file. You can use the docker inspect
command to return low-level information on the container.
To get the log path of a container instance, you can use the below command:
docker inspect --format='{{.LogPath}}' $INSTANCE_ID
For example, it retrieves the log path similar to below:
> docker inspect --format='{{.LogPath}}' dazzling_cray /var/lib/docker/containers/36ef4edbf9898d9cc1abe13d8dd0916eff3b44a9e92fdd6f078842a7157e2258/36ef4edbf9898d9cc1abe13d8dd0916eff3b44a9e92fdd6f078842a7157e2258-json.log
You can use cat
command to open the log file in your machine.

A drawback of this approach is that the log statements are in JSON formats and may be less readable.
{"log":"Hi\n","stream":"stdout","time":"2022-12-22T02:27:01.5556507Z"} {"log":"hihoi\n","stream":"stdout","time":"2022-12-22T03:49:48.9149033Z"}
3.2. Docker logs to dump a file
You can use docker logs
to get logs of the container in the detached mode.
> docker logs -f 6d12d35591f4 &> output.log &
-f
(i.e.--follow
): writes all existing logs and follows logging everything that comes next.&>
redirects both the standard output and standard error to the file.- You can run in the background using the
&
at the end. - You can separate stdout and stderr by:
> output.log 2> error.log
(instead of using&>
).
> docker logs -f 6d12d35591f4 > output.log 2> error.log
4. Conclusion
To sum up, we have learned to dump docker logs.