152

I am running several containers using docker-compose. I can see application logs with command docker-compose logs. However I would like to access raw log file to send it somewhere for example? Where is it located? I guess it's separate log per each container (inside container?) but where I can find it?

user606521
  • 14,486
  • 30
  • 113
  • 204

9 Answers9

235

A container's logs can be found in :

/var/lib/docker/containers/<container id>/<container id>-json.log

(if you use the default log format which is json)

Pierre
  • 2,552
  • 5
  • 26
  • 47
Michael
  • 8,229
  • 4
  • 17
  • 14
  • 6
    There are a lot of folders like `004279dd2985037950beeba7e6fe45c10354476d3b82afb68e72dd612b03a8ff`. How to know which folder to look at for a particular container? – Praveen Sripati Sep 30 '18 at 23:40
  • 4
    @PraveenSripati the container id for running containers is shown in the first column if you do `docker ps` – C S Oct 03 '18 at 19:09
  • 2
    @PraveenSripati write `docker ps`, you'll get all of your available containers along with their IDs. Copy the ID of your desired container, then from `/var/lib/docker/containers/` run `ls | grep `. Then you'll see that docker container – TheLebDev Sep 09 '19 at 07:12
  • 1
    `cd /var/lib/docker/containers/` **-bash: cd: /var/lib/docker/containers/: Permission denied** `sudo cd /var/lib/docker/containers/` **sudo: cd: command not found** – canbax Nov 19 '19 at 08:53
  • 3
    @Chris I had to do `docker ps --no-trunc` in order to see the entire id – Dan Z Dec 09 '19 at 02:57
  • @DanZ how come? what would the command output without `no-trunc`? – TheLebDev Dec 09 '19 at 08:24
  • @TheLebDev `CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES` ... `6666980fe505 amazon/amazon-ecs-agent:latest "/agent" 15 hours ago Up 15 hours (healthy) ecs-agent`. I'm on Amazon Linux AMI, connecting with OpenSSH_for_Windows_7.7p1 – Dan Z Dec 09 '19 at 11:30
  • For stopped or exited containers try ```docker ps -a```. Take the container id and like other answers grep that id in the /var/lib/docker/containers dir or substitute it into this command ```cat /var/lib/docker/containers//-json.log``` – Norbert Jan 12 '21 at 21:33
122

You can docker inspect each container to see where their logs are:

docker inspect --format='{{.LogPath}}' $INSTANCE_ID

And, in case you were trying to figure out where the logs were to manage their collective size, or adjust parameters of the logging itself you will find the following relevant.

Fixing the amount of space reserved for the logs

This is taken from Request for the ability to clear log history (issue 1083)):

Docker 1.8 and docker-compose 1.4 there is already exists a method to limit log size using docker compose log driver and log-opt max-size:

mycontainer:
  ...
  log_driver: "json-file"
  log_opt:
    # limit logs to 2MB (20 rotations of 100K each)
    max-size: "100k"
    max-file: "20"

In docker compose files of version '2' , the syntax changed a bit:

version: '2'
...
mycontainer:
  ...
  logging:
    #limit logs to 200MB (4rotations of 50M each)
    driver: "json-file"
    options:
      max-size: "50m"
      max-file: "4"

(note that in both syntaxes, the numbers are expressed as strings, in quotes)

Possible issue with docker-compose logs not terminating

  • issue 1866: command logs doesn't exit if the container is already stopped
init_js
  • 4,143
  • 2
  • 23
  • 53
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    The short version: docker inspect -f '{{.LogPath}}' $INSTANCE_ID – Bienvenido David Mar 17 '18 at 02:57
  • from my experience, the `.log.*` log files are from the current day. Where does docker log driver store old logs (from previous days), or in case clearing old logs is configured, which configuration is it ? – Veverke Mar 14 '23 at 10:14
  • @Veverke I do not have access to a Docker environment to check that, but by default, the Docker Compose log driver stores logs in the `/var/lib/docker/containers//-json.log` file for each container. These logs are rotated daily and compressed, with one file per day kept. The rotated files are named `.log.*`, where the `*` represents the date of the log file in the format `YYYY-MM-DD`. For example, a log file for a container with ID `abc123` from March 12, 2023, would be named `abc123.log.2023-03-12`. – VonC Mar 14 '23 at 14:42
19

To see how much space each container's log is taking up, use this:

docker ps -qa | xargs docker inspect --format='{{.LogPath}}' | xargs ls -hl

(you might need a sudo before ls).

That Brazilian Guy
  • 3,328
  • 5
  • 31
  • 49
  • I get a permission denied. – Shakedk Dec 06 '18 at 08:09
  • 1
    Adding sudo at the end helped: `docker ps -qa | xargs docker inspect --format='{{.LogPath}}' | xargs sudo du -hl` – Shakedk Dec 06 '18 at 08:26
  • @Shakedk to fix your permission thing this will be helpful. `sudo groupadd docker` `sudo usermod -aG docker $USER` activate the changes to groups: `newgrp docker` `sudo chown "$USER":"$USER" /home/"$USER"/.docker -R` `sudo chmod g+rwx "$HOME/.docker" -R` – Elshan Apr 26 '22 at 03:23
10
docker inspect <containername> | grep log
Vasili Pascal
  • 3,102
  • 1
  • 27
  • 21
4

On Windows, the default location is: C:\ProgramData\Docker\containers\<container-id>-json.log.

pmohandas
  • 3,669
  • 2
  • 23
  • 25
  • This is the one when running a local container for development! thanks - it was very hard to find this info – Percy Jul 20 '20 at 15:36
3

Here is the location for

Windows 10 + WSL 2 (Ubuntu 20.04), Docker version 20.10.2, build 2291f61

Lets say

DOCKER_ARTIFACTS == \\wsl$\docker-desktop-data\version-pack-data\community\docker

Location of container logs can be found in

DOCKER_ARTIFACTS\containers\[Your_container_ID]\[Your_container_ID]-json.log

Here is an example

enter image description here

craftsmannadeem
  • 2,665
  • 26
  • 22
2

To directly view the logfile in less, I use:

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

run as ./viewLogs.sh CONTAINERNAME

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
icyerasor
  • 4,973
  • 1
  • 43
  • 52
1

As of 8/22/2018, the logs can be found in :

/data/docker/containers/<container id>/<container id>-json.log
e2_71828
  • 21
  • 2
0

To see the size of logs per container, you can use this bash command :

for cont_id in $(docker ps -aq); do cont_name=$(docker ps | grep $cont_id | awk '{ print $NF }') && cont_size=$(docker inspect --format='{{.LogPath}}' $cont_id | xargs sudo ls -hl | awk '{ print $5 }') && echo "$cont_name ($cont_id): $cont_size"; done

Example output:

container_name (6eed984b29da): 13M
elegant_albattani (acd8f73aa31e): 2.3G
Mathieu Rollet
  • 2,016
  • 2
  • 18
  • 31