Logging - How To See The Logs of A Docker Container - Stack Overflow
Logging - How To See The Logs of A Docker Container - Stack Overflow
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
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
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
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:
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 :
If the logs are large, then try considering to save logs into to a file:
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
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).
Let's try using that docker create start and then logs command again and see what happens.
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
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