Operations 8 min read

Master Docker Logging and Graylog Integration: A Step‑by‑Step Guide

This guide explains how Docker captures container output, stores it as JSON logs, configures various log drivers, and integrates with Graylog for centralized log management, including deployment, input setup, and sending logs from containers via Docker run or docker‑compose.

Efficient Ops
Efficient Ops
Efficient Ops
Master Docker Logging and Graylog Integration: A Step‑by‑Step Guide

Docker Logs

When a container starts, it runs as a child process of the Docker daemon, which captures the container's standard output and forwards it to a LogDriver. The default driver writes JSON lines to a local file, but other drivers such as syslog can be used.

Docker stores each log line as a JSON object, for example:

<code>{"log":"root@74205cdc7b53dd:/#ls\r\n","stream":"stdout","time":"xxx.155834526Z"}</code>

The log driver can be changed at container start‑up; supported drivers are shown in the following image:

Graylog Log Management

Graylog is an open‑source log management platform similar to the ELK stack. Docker natively supports the Graylog (GELF) protocol, and Graylog provides Docker images and a docker‑compose file for quick deployment.

Deploying Graylog

<code>mkdir graylog
cd graylog</code>

Initialize configuration files:

<code>mkdir -p ./graylog/config
cd ./graylog/config
wget https://raw.githubusercontent.com/Graylog2/graylog-docker/3.1/config/graylog.conf
wget https://raw.githubusercontent.com/Graylog2/graylog-docker/3.1/config/log4j2.xml
# Fix permission issue caused by Graylog using UID 1100
chown -R 1100:1100 ./graylog/config</code>

Edit

graylog.conf

to set the desired timezone, e.g.:

<code>root_timezone = Asia/Shanghai</code>

Create a

docker-compose.yml

file (excerpt):

<code>version: '3'
services:
  mongo:
    image: mongo:3
    networks:
      - graylog
    volumes:
      - mongo_data:/data/db
  elasticsearch:
    image: docker.elastic.co/elasticsearch/elasticsearch:7.5.0
    volumes:
      - es_data:/usr/share/elasticsearch/data
    environment:
      - http.host=0.0.0.0
      - transport.host=localhost
      - network.host=0.0.0.0
      - ES_JAVA_OPTS=-Xms512m -Xmx512m
    ulimits:
      memlock:
        soft: -1
        hard: -1
    networks:
      - graylog
  graylog:
    image: graylog/graylog:3.1
    volumes:
      - graylog_journal:/usr/share/graylog/data/journal
      - ./graylog/config:/usr/share/graylog/data/config
    environment:
      - GRAYLOG_PASSWORD_SECRET=somepasswordpepper
      - GRAYLOG_ROOT_PASSWORD_SHA2=8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918
      - GRAYLOG_HTTP_EXTERNAL_URI=http://192.168.0.103:9000/
    networks:
      - graylog
    depends_on:
      - mongo
      - elasticsearch
    ports:
      - 9000:9000
      - 1514:1514
      - 1514:1514/udp
      - 12201:12201
      - 12201:12201/udp
networks:
  graylog:
    driver: bridge
volumes:
  mongo_data:
    driver: local
  es_data:
    driver: local
  graylog_journal:
    driver: local</code>

Start the stack with

docker-compose up

and access the Graylog web UI at

http://<em>host</em>:9000

(default credentials admin/admin).

Configuring Graylog Input

In Graylog’s web UI, go to the System tab → Inputs and create a new input, e.g., GELF UDP. After saving, the input status changes to RUNNING and can receive log messages.

Sending Docker Logs to Graylog

Run a container with the GELF driver:

<code>docker run --log-driver=gelf \
  --log-opt gelf-address=udp://<em>graylog_server</em>:12201 \
  --log-opt tag="<em>container_tag</em>" \
  <em>IMAGE</em> <em>COMMAND</em></code>

Example:

<code>docker run -d \
  --log-driver=gelf \
  --log-opt gelf-address=udp://localhost:12201 \
  --log-opt tag="{{.ImageName}}/{{.Name}}/{{.ID}}" \
  busybox sh -c 'while true; do echo "Graylog test message"; sleep 10; done'</code>

When using

docker‑compose

, add logging configuration:

<code>logging:
  driver: "gelf"
  options:
    gelf-address: "udp://<em>graylog_server</em>:12201"
    tag: "<em>container_tag</em>"</code>

After logs are sent, they can be searched in Graylog’s Search tab.

DockeroperationscontainerloggingDocker ComposeGraylogLog Driver
Efficient Ops
Written by

Efficient Ops

This public account is maintained by Xiaotianguo and friends, regularly publishing widely-read original technical articles. We focus on operations transformation and accompany you throughout your operations career, growing together happily.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.