{"id":4158,"library":"opentelemetry-resourcedetector-docker","title":"OpenTelemetry Docker Resource Detector","description":"opentelemetry-resourcedetector-docker is a Python package for OpenTelemetry that automatically enriches telemetry data with resource attributes derived from the Docker container environment where the application is running. Currently at version 0.4.0, it is actively maintained with releases as needed to align with OpenTelemetry Python SDK and address Docker-related detection specifics.","status":"active","version":"0.4.0","language":"en","source_language":"en","source_url":"https://github.com/chrisguidry/opentelemetry-resourcedetector-docker","tags":["opentelemetry","docker","observability","tracing","metrics","resource-detection"],"install":[{"cmd":"pip install opentelemetry-resourcedetector-docker","lang":"bash","label":"Install package"}],"dependencies":[],"imports":[{"note":"Main class for Docker resource detection.","symbol":"DockerResourceDetector","correct":"from opentelemetry_resourcedetector_docker import DockerResourceDetector"},{"note":"Required to combine multiple resource detectors.","symbol":"get_aggregated_resources","correct":"from opentelemetry.sdk.resources import get_aggregated_resources"}],"quickstart":{"code":"import os\nfrom opentelemetry.sdk.resources import get_aggregated_resources\nfrom opentelemetry_resourcedetector_docker import DockerResourceDetector\nfrom opentelemetry.sdk.trace import TracerProvider\nfrom opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor\n\n# Configure the resource detector. DockerResourceDetector requires access to the Docker socket.\n# Ensure your application has read access to /var/run/docker.sock (Linux) or similar.\n# For demonstration, we'll try to detect, but actual attributes depend on Docker environment.\nresource = get_aggregated_resources([\n    DockerResourceDetector()\n])\n\n# Optional: Add a service name if not detected by environment variables or other detectors\nif not resource.attributes.get('service.name'):\n    resource = resource.merge(Resource({'service.name': 'my-docker-app'}))\n\n# Set up a TracerProvider with the detected resource\nprovider = TracerProvider(resource=resource)\n\n# Configure a simple console exporter for demonstration\nspan_processor = SimpleSpanProcessor(ConsoleSpanExporter())\nprovider.add_span_processor(span_processor)\n\n# Set the global tracer provider\nfrom opentelemetry import trace\ntrace.set_tracer_provider(provider)\n\n# Get a tracer and create a span\ntracer = trace.get_tracer(__name__)\n\nwith tracer.start_as_current_span(\"my-docker-operation\") as span:\n    span.set_attribute(\"environment\", os.environ.get('APP_ENV', 'development'))\n    print(f\"Span created with resource attributes: {resource.attributes}\")\n    print(\"Look for 'container.id' or other Docker-related attributes in the output.\")","lang":"python","description":"This quickstart demonstrates how to initialize the OpenTelemetry SDK with the `DockerResourceDetector` to automatically gather Docker-related resource attributes. The collected attributes are then used by a `TracerProvider` and printed to the console via a `ConsoleSpanExporter`. Ensure Docker is running and the application has permissions to access the Docker socket for successful detection."},"warnings":[{"fix":"Ensure the user running the application has read permissions for the Docker socket. Mount the Docker socket into the container (e.g., `-v /var/run/docker.sock:/var/run/docker.sock`) and configure appropriate user/group permissions.","message":"The `DockerResourceDetector` requires read access to the Docker daemon socket (typically `/var/run/docker.sock` on Linux) to retrieve container metadata. Running applications or the OpenTelemetry Collector inside a container as a non-root user often necessitates explicit volume mounts and permissions (`--group-add <docker-gid>` or `runAsGroup` in Kubernetes) for the socket.","severity":"gotcha","affected_versions":"All"},{"fix":"Consider alternatives like manually setting `OTEL_RESOURCE_ATTRIBUTES` or using other detectors that might be available for macOS container environments if Docker-specific attributes are crucial.","message":"The Docker detector may not function as expected on macOS due to the virtualization layer of Docker Desktop, which can abstract the Docker socket access. The OpenTelemetry Collector's `resourcedetection` processor explicitly states that its Docker detector does not work on macOS.","severity":"gotcha","affected_versions":"All"},{"fix":"Verify that the Docker daemon is running and accessible from the application's environment. Check logs for connection errors (e.g., 'connect ETIMEDOUT') and ensure proper socket configuration.","message":"If the application cannot access the Docker daemon (e.g., due to the daemon not running, an incorrect socket path, or blocked network access), the `DockerResourceDetector` will fail to populate Docker-related resource attributes. This can lead to missing contextual information in your telemetry.","severity":"gotcha","affected_versions":"All"},{"fix":"Be mindful of the order of detectors when aggregating resources. If conflicts are observed, adjust the order or explicitly set desired attributes through other means (e.g., `OTEL_RESOURCE_ATTRIBUTES` environment variable, which takes precedence over code-based attributes).","message":"When combining `DockerResourceDetector` with other resource detectors using `get_aggregated_resources`, if multiple detectors try to set the same resource attribute key, the value from the detector listed later in the `get_aggregated_resources` array will typically override values from earlier detectors.","severity":"gotcha","affected_versions":"All"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}