Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using docker-compose with GELF log driver

According 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.

This works fine when I run my containers manually from the command line. For instance,

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

will send a log message to my Graylog2 server running on localhost which has a UDP input listener configured at port 12201.

Now, I want to use the same log options with docker-compose which, according to the docs, should be possible in principle. However, the docs do not mention any log formats but json-file, syslog and none and when I include something like

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

in my docker-compose.yml file then docker-compose up fails with:

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)

For the record, this was on docker-compose 1.4.0 and docker 1.8.1, build d12ea79.

Apparently, Docker and docker-compose are not at the same level of implementation here. I just found that this has already been solved and included in the Master branch on Github, see https://github.com/docker/compose/issues/1869 and https://github.com/docker/docker-py/pull/724 .

Is there any way to either re-install docker-compose from the current Master branch or to add this to an existing installation manually? I could not find the file where the commit goes to anywhere on my host...

like image 877
Dirk Avatar asked Sep 09 '15 12:09

Dirk


People also ask

What is GELF driver?

The gelf logging driver is a convenient format that is understood by a number of tools such as Graylog, Logstash, and Fluentd. Many tools use this format. In GELF, every log message is a dict with the following fields: version. host (who sent the message in the first place)

How can you run a container that uses a json-file driver?

To use the json-file driver as the default logging driver, set the log-driver and log-opts keys to appropriate values in the daemon. json file, which is located in /etc/docker/ on Linux hosts or C:\ProgramData\docker\config\ on Windows Server. If the file does not exist, create it first.

What is the default logging driver for Docker?

As a default, Docker uses the json-file logging driver, which caches container logs as JSON internally. In addition to using the logging drivers included with Docker, you can also implement and use logging driver plugins.


2 Answers

For Docker-Compose v2 services, the syntax has changed slightly, it's all under a new "logging" section now:

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"

One important note: The address for gelf-address needs to be the external address of the server, as Docker resolves this address through the host's network.

like image 50
nwinkler Avatar answered Oct 19 '22 09:10

nwinkler


The issue has been fixed.

I have just tested logging to graylog2 using docker-compose 1.5.0rc2

You can try with this working example 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"

When container starts there is a warning

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

But it does not seem to affect that messages are sent to graylog.

like image 40
Tomasz Sętkowski Avatar answered Oct 19 '22 09:10

Tomasz Sętkowski