Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see the logs of a docker container

Tags:

docker

logging

I have a simple code for which I have created a docker container and the status shows it running fine. Inside the code I have used some print() commands to print the data. I wanted to see that print command output.

For this I have seen docker logs . But it seems not to be working as it shows no logs. How to check logs.?

 $ sudo docker ps
CONTAINER ID        IMAGE                        COMMAND                  CREATED             STATUS              PORTS                                            NAMES
a3b3fd261b94        myfirstdocker                     "python3 ./my_script…"   22 minutes ago      Up 22 minutes                                                        elegant_darwin

 $ sudo docker logs a3b3fd261b94
 <shows nothing>
like image 340
S Andrew Avatar asked Dec 15 '17 09:12

S Andrew


2 Answers

The first point you need to print your logs to stdout.

To check docker logs just use the following command:

docker logs --help

Usage:  docker logs [OPTIONS] CONTAINER

Fetch the logs of a container

Options:
      --details        Show extra details provided to logs
  -f, --follow         Follow log output
      --help           Print usage
      --since string   Show logs since timestamp
      --tail string    Number of lines to show from the end of the logs (default "all")
  -t, --timestamps     Show timestamps

Some example:

docker logs --since=1h <container_id>
like image 72
nickgryg Avatar answered Nov 07 '22 21:11

nickgryg


If there's not so much supposed output (e.g. script just tries to print few bytes), I'd suspect python is buffering it.

Try adding more data to the output to be sure that buffer is flushed, and also using PYTHONUNBUFFERED=1 (although, python3 still may do some buffering despite of this setting).

like image 1
Hleb Rubanau Avatar answered Nov 07 '22 23:11

Hleb Rubanau