简体   繁体   中英

How can I capture STDOUT of a docker container to a file OUTSIDE of that container?

I have a docker container that when I run it, it automatically stops (because of the application crashing) and I cannot figure out why it is crashing because I have found no way to capture the STDOUT of the docker application to a file on the host file system. Without this, when my app crashes, my docker container shuts down and is removed automatically, so there is no container to attach to or get logs from.

I have found no help in the docker documentation for how to solve this issue.

There is a multitude of possibilities for this, some of them mentioned in the comments.

For me, doing redirects or even mounting volumes for the sake of logging is somewhat out of the docker scope. Instead, using a logdriver and checking the logs on the host or central log system seems to be the right approach.

This can be done in a pretty easy way. Let's assume you want to run a simple nginx container.

docker run -dt --name web01 -p 8080:80 docker.io/library/nginx:latest

This will do the logging as stated from your post (STDOUT of the container), which can be checked via docker logs .

If you want to have the log somewhere else, you run something like:

docker run -dt --name web01 --log-driver journald -p 8080:80 docker.io/library/nginx:latest

Which will produce log entries in journald.

I am not entirely sure how you want to get your hands on the logs, but all options are documented, very well.

https://docs.docker.com/config/containers/logging/configure/

You can even change the default logdriver (for all containers), if this is your desired behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM