Skip to content

feat: allow passing options to json-file Docker logging driver

What does this MR do?

This MR adds configurable log options for GitLab Runner's Docker executor, allowing users to specify custom logging configuration that gets applied to all Docker containers (build, service, and health check containers). The implementation:

  • Adds a new log_options configuration field to the Docker executor settings
  • Creates a GetLogConfig() helper method that maintains the required "json-file" log driver while applying configurable options
  • Updates all three Docker container creation functions to use the configurable log options instead of hardcoded empty configurations

Why was this MR needed?

Previously, GitLab Runner's Docker executor hardcoded the LogConfig to use only {Type: "json-file"} without any additional options, making it impossible to customize logging option like labels, env, or other json-file driver options. This prevented log forwarding to external systems with additional metadata such as job and project IDs or names. The hardcoded empty config also prevented inheriting useful settings from /etc/docker/daemon.json.

This was particularly problematic for users wanting to forward GitLab Runner logs to centralized logging systems like GCP Logging, which rely on container log attributes for proper log routing and metadata.

What's the best way to test this MR?

  1. Basic functionality test:

    [[runners]]
      [runners.docker]
        image = "alpine:latest"
        [runners.docker.log_options]
          labels = "com.gitlab.gitlab-runner.job.id,com.gitlab.gitlab-runner.project.id"
          env = "CI_JOB_ID,CI_JOB_NAME,CI_PROJECT_ID,CI_RUNNER_ID"
  2. Verify container log configuration:

    # Run a job and inspect the created containers
    docker inspect <container_id> | jq '.HostConfig.LogConfig'
  3. Check log files contain metadata:

    # Verify logs in /var/lib/docker/containers/*/
    # Should now include the configured labels and environment variables in the `attrs` field.
  4. Run unit tests:

    go test ./executors/docker -run "TestDockerConfigGetLogConfig|TestCreateHostConfigForServiceHealthCheckLogConfig"

FWIW, here is my Google Cloud Ops Agent configuration to consume this metadata:

      tee -a /etc/google-cloud-ops-agent/config.yaml > /dev/null <<EOF
      logging:
        receivers:
          docker_containers:
            type: files
            include_paths:
              - /var/lib/docker/containers/*/*-json.log
            record_log_file_path: true
            wildcard_refresh_interval: 10s

What are the relevant issue numbers?

N/A

Edited by Patrick Decat

Merge request reports

Loading