Docker Community Forums

Share and learn in the Docker community.

Docker logs inside the docker container?


#1

Hi,

I am trying to find the logs that when you type “docker logs containername” outputs. I am trying to find it from inside the container itself. I.e. “docker exec -it containername bash”. I have tried “docker inspect --format=’{{.LogPath}}’ containerid” and it prints “/var/lib/docker/containers/containerid-json.log”, when I exec into the container and cd to “var/lib/docker” it says the file directory doesn’t exist?

The logs I believed were in stdout, however, I can’t seem to read them. I put a symlink on it to another log, and it doesn’t output anything?

Can somebody help, not sure if the above is achievable, but would’ve thought that this log is sitting inside the container somewhere?

Thank you.


(Gary Forghetti) #2

The docker container’s logs are not stored in the container. They are stored on the docker node in the directory /var/lib/docker/containers/.

$ docker container run -it hello-world
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
1b930d010525: Pull complete
Digest: sha256:2557e3c07ed1e38f26e389462d03ed943586f744621577a99efb77324b0fe535
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Get the container id.

# docker container ls -a --no-trunc
CONTAINER ID                                                       IMAGE               COMMAND             CREATED             STATUS                     PORTS               NAMES
68f198ec8159c040173f5cc0287dfbd58e43b773da909042a5cf19beaa78e515   hello-world         "/hello"            5 minutes ago       Exited (0) 5 minutes ago                       inspiring_minsky

That container’s logs are then located here on the docker node: /var/lib/docker/containers/68f198ec8159c040173f5cc0287dfbd58e43b773da909042a5cf19beaa78e515/68f198ec8159c040173f5cc0287dfbd58e43b773da909042a5cf19beaa78e515-json.log

# cat /var/lib/docker/containers/68f198ec8159c040173f5cc0287dfbd58e43b773da909042a5cf19beaa78e515/68f198ec8159c040173f5cc0287dfbd58e43b773da909042a5cf19beaa78e515-json.log
{"log":"\r\n","stream":"stdout","time":"2019-02-01T03:43:43.648933649Z"}
{"log":"Hello from Docker!\r\n","stream":"stdout","time":"2019-02-01T03:43:43.648976724Z"}
{"log":"This message shows that your installation appears to be working correctly.\r\n","stream":"stdout","time":"2019-02-01T03:43:43.64897985Z"}
{"log":"\r\n","stream":"stdout","time":"2019-02-01T03:43:43.648981896Z"}
{"log":"To generate this message, Docker took the following steps:\r\n","stream":"stdout","time":"2019-02-01T03:43:43.648983741Z"}
{"log":" 1. The Docker client contacted the Docker daemon.\r\n","stream":"stdout","time":"2019-02-01T03:43:43.648985618Z"}
{"log":" 2. The Docker daemon pulled the \"hello-world\" image from the Docker Hub.\r\n","stream":"stdout","time":"2019-02-01T03:43:43.648987499Z"}
{"log":"    (amd64)\r\n","stream":"stdout","time":"2019-02-01T03:43:43.648989529Z"}
{"log":" 3. The Docker daemon created a new container from that image which runs the\r\n","stream":"stdout","time":"2019-02-01T03:43:43.64899137Z"}
{"log":"    executable that produces the output you are currently reading.\r\n","stream":"stdout","time":"2019-02-01T03:43:43.6489933Z"}
{"log":" 4. The Docker daemon streamed that output to the Docker client, which sent it\r\n","stream":"stdout","time":"2019-02-01T03:43:43.648995264Z"}
{"log":"    to your terminal.\r\n","stream":"stdout","time":"2019-02-01T03:43:43.64899717Z"}
{"log":"\r\n","stream":"stdout","time":"2019-02-01T03:43:43.648998977Z"}
{"log":"To try something more ambitious, you can run an Ubuntu container with:\r\n","stream":"stdout","time":"2019-02-01T03:43:43.649000749Z"}
{"log":" $ docker run -it ubuntu bash\r\n","stream":"stdout","time":"2019-02-01T03:43:43.649002642Z"}
{"log":"\r\n","stream":"stdout","time":"2019-02-01T03:43:43.6490045Z"}
{"log":"Share images, automate workflows, and more with a free Docker ID:\r\n","stream":"stdout","time":"2019-02-01T03:43:43.64900631Z"}
{"log":" https://hub.docker.com/\r\n","stream":"stdout","time":"2019-02-01T03:43:43.649009798Z"}
{"log":"\r\n","stream":"stdout","time":"2019-02-01T03:43:43.649011696Z"}
{"log":"For more examples and ideas, visit:\r\n","stream":"stdout","time":"2019-02-01T03:43:43.649013472Z"}
{"log":" https://docs.docker.com/get-started/\r\n","stream":"stdout","time":"2019-02-01T03:43:43.649015347Z"}
{"log":"\r\n","stream":"stdout","time":"2019-02-01T03:43:43.649017234Z"}

To print just the messages:

cat /var/lib/docker/containers/68f198ec8159c040173f5cc0287dfbd58e43b773da909042a5cf19beaa78e515/68f198ec8159c040173f5cc0287dfbd58e43b773da909042a5cf19beaa78e515-json.log | jq -r '.log'


Hello from Docker!

This message shows that your installation appears to be working correctly.



To generate this message, Docker took the following steps:

 1. The Docker client contacted the Docker daemon.

 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.

    (amd64)

 3. The Docker daemon created a new container from that image which runs the

    executable that produces the output you are currently reading.

 4. The Docker daemon streamed that output to the Docker client, which sent it

    to your terminal.



To try something more ambitious, you can run an Ubuntu container with:

 $ docker run -it ubuntu bash



Share images, automate workflows, and more with a free Docker ID:

 https://hub.docker.com/



For more examples and ideas, visit:

 https://docs.docker.com/get-started/