Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Logging - How To See The Logs of A Docker Container - Stack Overflow

Uploaded by

fabianlowe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Logging - How To See The Logs of A Docker Container - Stack Overflow

Uploaded by

fabianlowe
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

7/11/24, 4:06 PM logging - How to see the logs of a docker container - Stack Overflow

How to see the logs of a docker container


Asked 6 years, 7 months ago Modified 3 months ago Viewed 168k times

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
56 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>

docker logging

Share Follow edited Dec 15, 2017 at 9:36 asked Dec 15, 2017 at 9:30
S Andrew
6,556 32 138 265

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. – tgogos Dec 15, 2017 at 9:33

@tgogos I have added some more information – S Andrew Dec 15, 2017 at 9: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, 2017 at 9:49

Please refer stackoverflow.com/questions/29663459/… – Vamshi krishna Srirangam Feb 23, 2023 at 10:23

Please refer this stackoverflow.com/questions/29663459/… – Vamshi krishna Srirangam Feb 23, 2023 at
10:25

4 Answers Sorted by: Highest score (default)

https://stackoverflow.com/questions/47829345/how-to-see-the-logs-of-a-docker-container 1/4
7/11/24, 4:06 PM logging - How to see the logs of a docker container - Stack Overflow

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

To check docker logs just use the following command:


81
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>

Share Follow answered Dec 15, 2017 at 9:36


nickgryg
27.5k 6 80 82

1 I have a python code and inside that I have used print() . I hope stdout is same as print()
– S Andrew Dec 15, 2017 at 9:38

Yes, you are right. print() sends outpot to stdout . – nickgryg Dec 15, 2017 at 9:59

@SAndrew I am getting same problem. were you able to resolve this.? – PGS Jan 17, 2019 at 4: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, 2019 at 5:18

1 Ok Thanks. I will try the same.But i see when we do docker logs -f <container> logs are getting
printed after sometime.I think there is buffer in docker and periodically prints logs. – PGS Jan 17, 2019 at
5:32

To retrieve Docker logs from the last hour for a specific container with container_id :

3 docker logs --since=1h 'container_id'

If the logs are large, then try considering to save logs into to a file:

docker logs --since=1h 'container_id' > /path/to/save.txt

https://stackoverflow.com/questions/47829345/how-to-see-the-logs-of-a-docker-container 2/4
7/11/24, 4:06 PM logging - How to see the logs of a docker container - Stack Overflow

Share Follow edited Mar 26 at 20:56 answered Mar 4 at 6:53


balu k
4,488 2 19 31

If there's not so much supposed output (e.g. script just tries to print few bytes), I'd suspect
python is buffering it.
1
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).

Share Follow answered Dec 15, 2017 at 18:39


Hleb Rubanau
127 2

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

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.

https://stackoverflow.com/questions/47829345/how-to-see-the-logs-of-a-docker-container 3/4
7/11/24, 4:06 PM logging - How to see the logs of a docker container - Stack Overflow

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

Share Follow answered Mar 21, 2021 at 18:06


Rafiq
10.8k 4 42 43

10 Please use markdown code blocks instead of screen shots for the examples. – knia Apr 1, 2023 at 13:52

https://stackoverflow.com/questions/47829345/how-to-see-the-logs-of-a-docker-container 4/4

You might also like