S Andrew
S Andrew

Reputation: 7288

How to see the logs of a docker container

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>

Upvotes: 94

Views: 305216

Answers (5)

shivakrishna karnati
shivakrishna karnati

Reputation: 71

To check the status in linux: docker ps -a

To check the logs in linux: docker logs container_name | less

Upvotes: 1

balu k
balu k

Reputation: 5088

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

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

Upvotes: 9

Rafiq
Rafiq

Reputation: 11535

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

Upvotes: 2

Hleb Rubanau
Hleb Rubanau

Reputation: 137

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

Upvotes: 1

nickgryg
nickgryg

Reputation: 28743

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>

Upvotes: 130

Related Questions