访问被杀死的Docker容器的日志 [英] Access logs of a killed docker container

查看:412
本文介绍了访问被杀死的Docker容器的日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于过去的Docker容器占用了空间,最近我不得不清理我的完整磁盘.因此,我假设我可以访问已终止容器的日志.

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.

例如,我有一个容器的docker历史记录:

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:我对Docker不太了解,我接手了一个项目.每次更新代码后重新启动容器的方式是:

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

推荐答案

默认情况下,销毁容器也将删除日志.如果需要日志,则必须指定--log-driver选项.在现代的GNU/Linux机器上,使用日记命令,例如,使用docker run命令

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

另一个使用docker-compose.yml语法的示例:

Another example using docker-compose.yml syntax :

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

然后使用journalctl命令+过滤规则访问日志

Then access logs using journalctl command + filter rules

journalctl -u docker CONTAINER_NAME=mycontainer_name

journalctl -u docker CONTAINER_TAG=mytag

在运行多服务应用程序(例如,使用docker-compose)时,标记很有用.

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

我认为在您的情况下,使用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.

此外,Docker历史记录命令链接到映像,而不是容器(容器==运行指定映像的实例)

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

这篇关于访问被杀死的Docker容器的日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆