Docker容器将日志保存在主机目录中 [英] Docker container save logs on the host directory

查看:136
本文介绍了Docker容器将日志保存在主机目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似于此问题的问题.当运行我的 docker-compose.yml 文件时,我会自动创建一个docker映像,并按照我的 dockerfile 中的指定运行某些应用程序.这些应用程序会生成一些日志,但是这些日志会写在docker容器的内部内的/home/logs/文件夹中.

I have a question similar to this one. When run my docker-compose.yml file, I automatically create a docker image and as specified in my dockerfile, I run certain apps. These apps produce some logs, but these logs are written inside the docker container, in /home/logs/ folder.

我如何指示这些日志写在我的/path/on/host 地址上的外部容器中?仅仅是因为如果容器发生故障,我需要查看日志而不丢失它们!

How can I indicate that these logs be writted outside the container, on my /path/on/host address? Simply because if the container was failed, I need to see the logs and not lose them!

这是我的 docker-compose.yml :

version: '3'
services:
  myapp:
    build: .
    image: myapp
    ports:
      - "9001:9001"

这是我的 dockerfile :

FROM java:latest
COPY myapp-1.0.jar /home
CMD java -jar /home/myapp-1.0.jar

然后我只需使用 docker-compose up -d 在生产机器上运行它即可.

And I simply run it on production machine with docker-compose up -d.

(顺便说一句,我是Docker的新手.我的所有步骤都正确吗?我是否遗漏了任何东西?!我发现一切都很好,尽管myapp正在运行!)

(BTW, I'm new to dockers. Are all of my steps correct? Am I missing anything?! I see all is fine and myapp is running though!)

推荐答案

您所需要的只是一个docker卷,以保留日志文件.因此,在与docker-compose.yml相同的目录中创建一个日志目录,然后定义卷安装.定义安装座时,请记住语法为< host_machine_directy>:< container_directory> .

All you need is a docker volume in order to persist the log files. So in the same directory as your docker-compose.yml create a logs directory, then define a volume mount. When defining a mount remember the syntax is <host_machine_directy>:<container_directory>.

尝试以下内容,让我知道您的收获.

Give the following volume a try and let me know what you get back.

version: '3'
services:
  myapp:
    build: .
    image: myapp
    ports:
      - "9001:9001"
    volumes:
      - ./logs:/home/logs

同样值得注意的是,这种方法的持久性是双向的.从容器内部对文件所做的任何更改都将反映回主机.来自主机的任何更改也将反映在容器内部.

Also worth noting that persistence goes both ways with this approach. Any changes made to the files from within the container are reflected back onto the host. Any changes from the host, are also reflected inside the container.

这篇关于Docker容器将日志保存在主机目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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