logging - 将 docker-compose 与 GELF 日志驱动程序一起使用

标签 logging docker docker-compose

根据 to the official Docker docs ,可以获得容器的 stdoutstderr 输出为 GELF messages这是一种可以理解的格式,例如Graylog / Graylog2logstash .

当我从命令行手动运行我的容器时,这可以正常工作。例如,

docker run --log-driver=gelf --log-opt gelf-address=udp://localhost:12201 busybox echo This is my  message.

将向我在 localhost 上运行的 Graylog2 服务器发送一条日志消息,该服务器在端口 12201 上配置了一个 UDP 输入监听器。

现在,我想使用与 docker-compose 相同的日志选项其中,根据文档,should be possible in principle .但是,文档没有提到任何日志格式,但 json-filesyslognone 以及当我包含类似

的内容时
my-container:
  container_name: ...
  build: ...
  ports: ...
  log_driver: "gelf"
  log_opt:
    gelf-address: "udp://localhost:12201"

在我的 docker-compose.yml 文件中,然后 docker-compose up 失败:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "/code/build/docker-compose/out00-PYZ.pyz/compose.cli.main", line 39, in main
  File "/code/build/docker-compose/out00-PYZ.pyz/compose.cli.docopt_command", line 21, in sys_dispatch
  File "/code/build/docker-compose/out00-PYZ.pyz/compose.cli.command", line 27, in dispatch
  File "/code/build/docker-compose/out00-PYZ.pyz/compose.cli.docopt_command", line 24, in dispatch
  File "/code/build/docker-compose/out00-PYZ.pyz/compose.cli.command", line 59, in perform_command
  File "/code/build/docker-compose/out00-PYZ.pyz/compose.cli.main", line 495, in up
  File "/code/build/docker-compose/out00-PYZ.pyz/compose.project", line 265, in up
  File "/code/build/docker-compose/out00-PYZ.pyz/compose.service", line 369, in execute_convergence_plan
  File "/code/build/docker-compose/out00-PYZ.pyz/compose.service", line 270, in create_container
  File "/code/build/docker-compose/out00-PYZ.pyz/compose.service", line 643, in _get_container_create_options
  File "/code/build/docker-compose/out00-PYZ.pyz/compose.service", line 656, in _get_container_host_config
  File "/code/build/docker-compose/out00-PYZ.pyz/docker.utils.types", line 27, in __init__
ValueError: LogConfig.type must be one of (json-file, syslog, none)

据记录,这是在 docker-compose 1.4.0 和 docker 1.8.1 上构建的 d12ea79。

显然,这里的 Docker 和 docker-compose 不在同一个实现级别。我刚刚发现这个问题已经解决并包含在 Github 上的 Master 分支 中,见 https://github.com/docker/compose/issues/1869https://github.com/docker/docker-py/pull/724 .

有没有办法从当前的 Master 分支重新安装 docker-compose 或手动将其添加到现有安装中?我在我的主机上找不到提交到任何地方的文件...

最佳答案

对于 Docker-Compose v2 服务,语法略有变化,现在都在一个新的“日志记录”部分下:

version: "2"

services:
  example:
    container_name: example
    image: debian:wheezy
    command: /bin/sh -c "while true; do date && echo "hello"; sleep 1; done"
    ports:
      - "1234:1234"
    logging:
      driver: "gelf"
      options:
        gelf-address: "udp://graylog.example.com:12201"
        tag: "first-logs"

一个重要说明:gelf-address的地址需要是服务器的外部地址,因为Docker通过主机的网络解析这个地址。

关于logging - 将 docker-compose 与 GELF 日志驱动程序一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32479530/

相关文章:

docker - 如何阻止对docker容器linux centos 7的外部访问

tomcat - 如何配置tomcat以翻转catalina.log文件

java - 登录 AppEngine

python - 在基于 Alpine Linux 的 Docker 镜像中安装 pylint

docker - 如何使用gitlab和digitalocean的服务器添加暂存和生产环境

docker - 错误层=rpc 写入响应 :write tcp 127. 0.0.1 :40000->127. 0.0.1:41558:使用关闭的网络连接

c++ - 自动将进入/退出功能日志添加到项目中

logging - 如何修复JSP编译器警告: one JAR was scanned for TLDs yet contained no TLDs?

docker - 为什么在 docker-compose 中我应该写卷的完整路径?

asp.net - 从 ASP.NET Core Controller 读取 docker-compose 环境变量