Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see the logs of running application inside docker container?

Tags:

node.js

docker

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.

like image 429
madcolonel10 Avatar asked Jun 25 '17 07:06

madcolonel10


People also ask

How do I check running container logs?

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.


2 Answers

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.

like image 121
jsalonen Avatar answered Nov 14 '22 22:11

jsalonen


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/

like image 33
Eugen Mayer Avatar answered Nov 14 '22 22:11

Eugen Mayer