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

Dir*_*irk 16 logging docker docker-compose

根据官方Docker文档,可以将容器的输出stdoutstderr输出作为GELF消息获取,这是格式,例如Graylog/Graylog2logstash.

当我从命令行手动运行容器时,这很好用.例如,

docker run --log-driver=gelf --log-opt gelf-address=udp://localhost:12201 busybox echo This is my  message.
Run Code Online (Sandbox Code Playgroud)

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

现在,我想使用与docker-compose相同的日志选项,根据文档,原则上应该是可能的.但是,文档不提任何日志格式,但是json-file,syslognone当我包括像

my-container:
  container_name: ...
  build: ...
  ports: ...
  log_driver: "gelf"
  log_opt:
    gelf-address: "udp://localhost:12201"
Run Code Online (Sandbox Code Playgroud)

在我的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)
Run Code Online (Sandbox Code Playgroud)

为了记录,这是在docker-compose 1.4.0和docker 1.8.1,build d12ea79上.

显然,Docker和docker-compose在这里的实现水平不同.我刚刚发现这已经解决并包含在Master branchGithub上,请参阅 https://github.com/docker/compose/issues/1869https://github.com/docker/docker-py/pull/724.

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

Tom*_*ski 12

问题已得到解决.

我刚刚使用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"
Run Code Online (Sandbox Code Playgroud)

当容器启动时会出现警告

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

但它似乎并没有影响将消息发送到graylog.

  • 包含该警告是因为您正在查看的输出是从与 `docker logs` 相同的来源生成的,这对于开箱日志驱动程序不可用。 (2认同)

nwi*_*ler 9

对于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"
Run Code Online (Sandbox Code Playgroud)

一个重要的注意事项:gelf-address需要作为服务器外部地址的地址,因为Docker通过主机网络解析此地址.