I have a Node.js application running inside Docker container in /usr/src/app.
I want to run this application using nohup (nohup node index.js &).
What is the best way to see the output appended to nohup.out for general/debugging purposes?
Do I need to use docker cp all the time to copy to host os. Is there a better way to achieve this?
Thanks.
Docker Command for Checking Container Logs Replace container_id with the ID number of the container you want to inspect. To find the container ID, use the docker ps command to list running containers. As in the image below, Docker responds by listing the event logs for that specific container in the output.
Although using logfiles inside container (without volume mounting) is an anti-pattern, you can easily output those files using exec for example along with cat:
docker exec -it [CONTAINERID] cat /usr/src/app/nohup.out
Better pattern would be to store output into separate volume-mounted folder removing state from inside your container, but would also allow you to access logs directly from host system.
In this specific case, I don't see a need for a separate logfile, and not even for nohup. Just set policy for your container to restart it automatically (--restart always
) instead of nohup.
You do output your logs in
/dev/stdout
for all normal logs
use /dev/stderr
to pipe all the errors you have, if your application can differ between normal logs and error logs.
So instead of defining your log like /var/log/nohup.log
you define the logfile to be dev/stdout
Accessing the logs will then be as easy as writing docker logs <containername>
or docker-compose logs <servicename>
- if you have started your stack using docker-compose up
you will see the logs right in front of you anyway - very convenient.
In production grade you want to process the log stream further, look at https://github.com/gliderlabs/logspout
And if you want to go a level deeper, add the ELK stack to it https://logz.io/learn/complete-guide-elk-stack/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With