使用docker-compose和GELF日志驱动程序

时间:2015-09-09 12:26:29

标签: logging docker docker-compose

根据to the official Docker docs,可以将容器的stdoutstderr输出设为GELF messages,这种格式可以通过例如{3}来理解。 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 branch中,请参阅 https://github.com/docker/compose/issues/1869https://github.com/docker/docker-py/pull/724

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

2 个答案:

答案 0 :(得分:11)

问题已得到解决。

我刚刚使用docker-compose 1.5.0rc2

测试了对graylog2的记录

您可以尝试使用这个工作示例yaml:

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

当容器启动时会出现警告

example | WARNING: no logs are available with the 'gelf' log driver

但似乎没有影响将消息发送到graylog。

答案 1 :(得分:7)

对于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通过主机网络解析此地址。