Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access logs of a killed docker container

Tags:

docker

logging

I recently had to clean up my full disk because of space taken by past docker containers. So I assume that I can access logs of killed containers.

For example I have the docker history of a container:

$ docker history xxx_app
IMAGE               CREATED             CREATED BY                                      SIZE                COMMENT
d7cfe17fc42a        56 minutes ago      /bin/sh -c #(nop)  EXPOSE 3000/tcp              0 B                 
cd26ca1108f0        56 minutes ago      /bin/sh -c #(nop) COPY dir:8daa84a931569267ab   62.27 MB            
6fa873fcc7bb        9 days ago          /bin/sh -c npm install && npm cache clean       177.8 MB            
67a23b0934d8        9 days ago          /bin/sh -c #(nop) COPY file:5dcb2a83410d0aa7f   1.529 kB            
3b7197885c91        3 weeks ago         /bin/sh -c #(nop)  ENV NODE_ENV=                0 B                 
79a447242ea5        3 weeks ago         /bin/sh -c #(nop)  ARG NODE_ENV                 0 B                 
b1909b86ce39        3 weeks ago         /bin/sh -c #(nop)  CMD ["npm" "start"]          0 B                 
<missing>           3 weeks ago         /bin/sh -c #(nop)  ONBUILD COPY . /usr/src/ap   0 B                 
<missing>           3 weeks ago         /bin/sh -c #(nop)  ONBUILD RUN npm install &&   0 B                 
<missing>           3 weeks ago         /bin/sh -c #(nop)  ONBUILD COPY package.json    0 B                 
<missing>           3 weeks ago         /bin/sh -c #(nop)  ONBUILD ENV NODE_ENV $NODE   0 B                 
<missing>           3 weeks ago         /bin/sh -c #(nop)  ONBUILD ARG NODE_ENV         0 B                 
<missing>           3 weeks ago         /bin/sh -c #(nop)  WORKDIR /usr/src/app         0 B                 
...

But I get an error when accessing logs:

docker logs 67a23b0934d8
Error: No such container: 67a23b0934d8

Despite that, my disk's getting full by container images that were created, and I had to take action following this article to clean up about a month ago. So, can I access past logs?

PS: I'm not very knowledgeable about docker, I took over a project. The way the containers are restarted after each code update is this:

docker-compose -f docker-compose-production.yaml down
docker-compose -f docker-compose-production.yaml up -d --build
like image 598
coyotte508 Avatar asked Apr 27 '17 16:04

coyotte508


People also ask

How can I see the logs of a stopped docker container?

First of all, to list all running containers, use the docker ps command. Then, with the docker logs command you can list the logs for a particular container. Most of the time you'll end up tailing these logs in real time, or checking the last few logs lines.

Can I access the logs of the docker container?

The docker logs command shows information logged by a running container. The docker service logs command shows information logged by all containers participating in a service. The information that is logged and the format of the log depends almost entirely on the container's endpoint command.

Where are the docker container logs?

By default, the Docker containers log files are stored in /var/lib/docker/containers/<containerId> dir. In addition, we can also redirect the Docker containers logs to some other file.

How to get Docker logs in batch?

We can use the Docker logs command to batch-retrieve the logs present during the execution of the containers. However, this command is functional only when you use the journald or json-file logging drivers. The syntax of the Docker logs command is – $ docker logs [OPTIONS] CONTAINER

How can I tail a log file inside a docker container?

Using this, you can tail a log file inside a Docker container: Because this lets you run any command, you can use journalctl or any other debugging strategies you want, as long as you preface it with docker exec -it. You can even run /bin/bash if you want to hop in and poke around.

What does the Docker Service logs command show?

The docker service logs command shows information logged by all containers participating in a service. The information that is logged and the format of the log depends almost entirely on the container’s endpoint command.

How to use Docker logs to track failures?

You can tail Docker logs to find the exact set of commands that were responsible for the failure. Docker logs also help you to monitor the processes inside the container by live-streaming the process details.


2 Answers

By default, destroying a container will also remove logs. If you need logs, you have to specify a --log-driver option. On a modern GNU/Linux box, use journald, for example with the docker run command

docker run --log-driver=journald

Another example using docker-compose.yml syntax :

mycontainer:
    image: myimage
    logging:
        driver: journald
        options:
            tag: mytag

Then access logs using journalctl command + filter rules

journalctl -u docker CONTAINER_NAME=mycontainer_name

journalctl -u docker CONTAINER_TAG=mytag

Tag is useful when you're running a multiple service application, for example with docker-compose.

I think in your case, the container is "recreated" using docker-compose so logs are linked to container lifetime if you don't specify logging-driver stuff.

Also, Docker history command is linked to an image, not a container (container == running instance of a specified image)

like image 128
papey Avatar answered Oct 21 '22 07:10

papey


As the question states that the container is killed not destroyed (removed), you can still access logs of not running containers doing docker logs <container-id>

You can find out the ID of the not running container with: docker ps -a

As long as you have the default docker logging driver.

like image 36
Robert Avatar answered Oct 21 '22 08:10

Robert