简体   繁体   English

Docker 容器将日志保存在主机目录

[英]Docker container save logs on the host directory

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.运行我docker-compose.yml文件时,我会自动创建一个dockerfile映像,并按照我的dockerfile指定运行某些应用程序。 These apps produce some logs, but these logs are written inside the docker container, in /home/logs/ folder.这些应用程序产生一些日志,但这些日志写入泊坞窗容器,在/home/logs/文件夹。

How can I indicate that these logs be writted outside the container, on my /path/on/host address?如何指示这些日志写在容器外部,在我的/path/on/host地址上? Simply because if the container was failed, I need to see the logs and not lose them!仅仅是因为如果容器发生故障,我需要查看日志而不是丢失它们!

This is my docker-compose.yml :这是我docker-compose.yml

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

and here is my dockerfile :这是我的dockerfile

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

And I simply run it on production machine with docker-compose up -d .我只是在生产机器上使用docker-compose up -d运行它。

(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!) (顺便说一句,我是码头工人的新手。我所有的步骤都正确吗?我错过了什么吗?!我看到一切都很好,但我的应用程序正在运行!)

All you need is a docker volume in order to persist the log files.您只需要一个 docker 卷即可保存日志文件。 So in the same directory as your docker-compose.yml create a logs directory, then define a volume mount.因此,在与 docker-compose.yml 相同的目录中创建一个日志目录,然后定义一个卷挂载。 When defining a mount remember the syntax is <host_machine_directy>:<container_directory> .定义挂载时,请记住语法是<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.来自主机的任何更改也会反映在容器内。

  • Yes , you can mount the volume from host into the container as outlined in the above answer , using a bind mount是的,您可以使用绑定安装将卷从主机装入容器,如上述答案中所述

  • In production , I would highly recommend sending logs of all containers to some central location , so that even if the whole docker host goes down you still have access to logs and maybe can easily analyse , filter, set watchers on log errors and make a dashboard , such as ELK在生产中,我强烈建议将所有容器的日志发送到某个中心位置,这样即使整个 docker 主机宕机,您仍然可以访问日志,并且可能可以轻松地分析、过滤、设置日志错误的观察者并制作仪表板,比如麋鹿

https://docs.docker.com/config/containers/logging/configure/https://docs.docker.com/config/containers/logging/configure/

For this to work , you need to configure the app to send logs to stdout instead , and then configure docker daemon to send logs to one of your end points such as logstash , then you can configure logstash to do some pre-processing ( if needed ) and then stream it to your elasticsearch instance.为此,您需要将应用程序配置为将日志发送到 stdout,然后配置 docker 守护程序将日志发送到您的端点之一,例如logstash ,然后您可以配置 logstash 进行一些预处理(如果需要) ),然后将其流式传输到您的elasticsearch实例。

Going one step further , you may consider a container management system such as kubernetes with central logging to ELK and metering to promethous .往前一步,你可以考虑容器管理体系,比如有中央记录到ELK和测光promethous kubernetes。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM