feat: allow passing env
and labels
options to json-file
Docker logging driver
-
Please check this box if this contribution uses AI-generated content (including content generated by GitLab Duo features) as outlined in the GitLab DCO & CLA. As a benefit of being a GitLab Community Contributor, you receive complimentary access to GitLab Duo.
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?
-
Basic functionality test:
[[runners]] [runners.docker] image = "alpine:latest" [runners.docker.log_options] labels = "com.gitlab.gitlab-runner.type" env = "CI_JOB_ID,CI_JOB_NAME,CI_PROJECT_ID,CI_RUNNER_ID"
-
Verify container log configuration:
# Run a job and inspect the created containers docker inspect <container_id> | jq '.HostConfig.LogConfig'
-
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.
-
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:
- /media/local0/var/lib/docker/containers/*/*-json.log
record_log_file_path: true
wildcard_refresh_interval: 10s
processors:
docker_json_parser:
type: parse_json
field: message
time_key: time
time_format: "%Y-%m-%dT%H:%M:%S.%LZ"
service:
pipelines:
docker_logs:
receivers: [docker_containers]
processors: [docker_json_parser]
What are the relevant issue numbers?
N/A