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

Using docker-compose with GELF log driver

根据to the official Docker docs, it is possible to get the stdout and stderr output of a container as GELF messages which is a format that is understood by e.g. Graylog / Graylog2 and logstash.

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

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

将在本地主机上向我的 Graylog2 服务器 运行ning 发送一条日志消息,该服务器在端口 12201 上配置了 UDP 输入侦听器。

现在,我想使用与 docker-compose which, according to the docs, 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.

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

问题已解决。

我刚刚使用 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。

对于 Docker-Compose v2 服务,语法略有变化,现在都在新的 "logging" 部分下:

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 通过主机的网络解析该地址。