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
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.
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.
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.
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
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.
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.
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.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With