196

Is there any way I can see the log of a container that has exited?

I can get the container id of the exited container using docker ps -a but I want to know what happened when it was running.

Knows Not Much
  • 30,395
  • 60
  • 197
  • 373
  • 1
    Possible duplicate of [Where is a log file with logs from a container?](https://stackoverflow.com/questions/33017329/where-is-a-log-file-with-logs-from-a-container) – icyerasor Oct 24 '17 at 13:54

5 Answers5

211

Use docker logs. It also works for stopped containers and captures the entire STDOUT and STDERR streams of the container's main process:

$ docker run -d --name test debian echo "Hello World"
02a279c37d5533ecde76976d7f9d1ca986b5e3ec03fac31a38e3dbed5ea65def

$ docker ps -a
CONTAINER ID    IMAGE     COMMAND        CREATED             STATUS                     PORTS               NAMES
49daa9d41a24    debian    "echo test"    2 minutes ago       Exited (0) 2 minutes ago                       test

$ docker logs -t test
2016-04-16T15:47:58.988748693Z Hello World
helmbert
  • 35,797
  • 13
  • 82
  • 95
  • 6
    For a docker stack where it restarts every few seconds: `stack=s1 && c=$(task_id=$(docker stack ps "$stack" --filter desired-state=shutdown | tail -n +2 | head -n 1 | awk '{print $1}') && docker inspect --format '{{.Status.ContainerStatus.ContainerID}}' "$task_id") && docker logs "$c"` Stack name is specified at the beginning of the command. – x-yuri Dec 13 '18 at 17:55
  • How would it work (IF it would) for a container that was started using the `--rm` flag? Is that possible? – M.Ionut Jan 19 '22 at 18:28
47

docker logs --tail=50 <container id> for the last fifty lines - useful when your container has been running for a long time.

Matt
  • 9,068
  • 12
  • 64
  • 84
kliew
  • 3,073
  • 1
  • 14
  • 25
  • 1
    @Whitefret, I've updated the answer with your suggestion. The original answer would pull the entire log down and tail it locally, would have taken a very long time with a big log and a slow network – Matt Apr 19 '18 at 09:16
20

You can use below command to copy logs even from an exited container :

docker cp container_name:path_of_file_in_container destination_path_locally

Eg:

docker cp sample_container:/tmp/report /root/mylog
Sravya
  • 661
  • 8
  • 9
  • 1
    I could use this to copy NPM debug logs out from a Python/Debian container that had stopped - the logs I was looking for weren't shown in the usual logs of Docker and were instead in the file system of Debian. – KeaganFouche May 21 '21 at 14:59
11

To directly view the logfile of an exited container in less, scrolled to the end of the file, I use:

docker inspect $1 | grep 'LogPath' | sed -n "s/^.*\(\/var.*\)\",$/\1/p" | xargs sudo less +G

run as ./viewLogs.sh CONTAINERNAME

This method has the benefit over docker logs based approaches, that the file is directly opened, instead of streamed.

sudo is necessary, as the LogPath/File usually is under root-owned

icyerasor
  • 4,973
  • 1
  • 43
  • 52
9

@icyerasor comment above actually helped me solve the issue. In my particular situation the container that has stopped running had no container name only container id.

Steps that found the logs also listed in this post

  1. Find the stopped container via docker ps -a
  2. grab the container id of the failed container
  3. Substitute it in this command cat /var/lib/docker/containers/<container id>/<container id>-json.log
northben
  • 5,448
  • 4
  • 35
  • 47
Norbert
  • 809
  • 9
  • 13