Docker look at the log of an exited container

2019-02-05 18:20发布

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.

标签: docker
4条回答
虎瘦雄心在
2楼-- · 2019-02-05 18:54

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 sravya:/tmp/report /root/mylog
查看更多
男人必须洒脱
3楼-- · 2019-02-05 18:58

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

查看更多
Fickle 薄情
4楼-- · 2019-02-05 19:02

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
查看更多
放荡不羁爱自由
5楼-- · 2019-02-05 19:09

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

查看更多
登录 后发表回答