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

根据官方的Docker文档 ,可以将容器的stdoutstderr输出作为GELF消息获得 , GELF消息是Graylog / Graylog2和logstash可以理解的格式。

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

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

将发送一个日志消息到我的本地主机上运行的Graylog2服务器,该主机有一个在端口12201configuration的UDPinput监听器。

现在,我想用docker-compose使用相同的日志选项,根据文档, 原则上应该是可能的 。 但是,文档没有提到任何日志格式,但json-filesyslognone ,当我包括类似的东西

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

在我docker-compose.yml文件中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) 

为了logging,这是docker-compose 1.4.0和docker 1.8.1,build d12ea79。

显然,Docker和Docker-compose在这里并不在同一级别上。 我刚刚发现这已经解决了,并且包含在Github的Master branch中,请参阅https://github.com/docker/compose/issues/1869和https://github.com/docker/docker-py/pull / 724 。

有没有办法从当前的主分支重新安装docker-compose或手动添加到现有的安装? 我无法find提交到我的主机上任何地方的文件…

问题已经解决。

我刚刚使用docker -compose 1.5.0rc2testing了对graylog2的日志logging

你可以尝试这个工作示例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" 

一个重要的注意事项:因为Docker通过主机的networkingparsing这个地址,所以gelf-address必须是服务器的外部地址。