29

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>
S Andrew
  • 5,592
  • 27
  • 115
  • 237
  • Try to add some more information to your question to help us recreate the problem. The ideal would be to provide a [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – tgogos Dec 15 '17 at 09:33
  • @tgogos I have added some more information – S Andrew Dec 15 '17 at 09:36
  • You have to make it more `complete` :-). As it says in the relative section: *" - Some people might be prepared to load the parts up, and actually try them to test the answer they're about to post. - The problem might not be in the part you suspect it is, but another part entirely."* – tgogos Dec 15 '17 at 09:49
  • Please refer https://stackoverflow.com/questions/29663459/python-app-does-not-print-anything-when-running-detached-in-docker – Vamshi krishna Srirangam Feb 23 '23 at 10:23
  • Please refer this https://stackoverflow.com/questions/29663459/python-app-does-not-print-anything-when-running-detached-in-docker – Vamshi krishna Srirangam Feb 23 '23 at 10:25

3 Answers3

42

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>
nickgryg
  • 25,567
  • 5
  • 77
  • 79
  • 1
    I have a python code and inside that I have used `print()`. I hope `stdout` is same as `print()` – S Andrew Dec 15 '17 at 09:38
  • Yes, you are right. `print()` sends outpot to `stdout`. – nickgryg Dec 15 '17 at 09:59
  • @SAndrew I am getting same problem. were you able to resolve this.? – PGS Jan 17 '19 at 04:54
  • @Gopi Instead of `print()`, I used the python logging module to save my logs in file and then I can see the logs – S Andrew Jan 17 '19 at 05:18
  • 1
    Ok Thanks. I will try the same.But i see when we do `docker logs -f ` logs are getting printed after sometime.I think there is buffer in docker and periodically prints logs. – PGS Jan 17 '19 at 05:32
0

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).

Hleb Rubanau
  • 117
  • 2
0

enter image description here4

Let's try using that docker create start and then logs command again and see what happens.

sudo docker create busybox echo hi there

output of the command enter image description here

now I will take the ID and run a docker start and paste the ID that starts up the container it executes echo high there inside of it and then immediately exits. enter image description here

Now I want to go back to that stopped container and get all the logs that have been emitted inside of it. To do so I can run at docker logs and then paste the ID in and I will see that when the container had been running it had printed out the string Hi there.

One thing to be really clear about is that by running docker logs I am not re-running or restarting the container to in any way shape or form, I am just getting a record of all the logs that have been emitted from that container. docker logs container_id enter image description here

Rafiq
  • 8,987
  • 4
  • 35
  • 35