Search code examples
pythondockerdatadog

Sending ddtrace from docker


I'm trying to learn how to use docker and am having some troubles. I'm using a docker-compose.yaml file for running a python script that connects to a mysql container and I'm trying to use ddtrace to send traces to datadog. I'm using the following image from this github page from datadog

ddagent:
      image: datadog/docker-dd-agent
      environment:
          - DD_BIND_HOST=0.0.0.0
          - DD_API_KEY=invalid_key_but_this_is_fine
      ports:
          - "127.0.0.1:8126:8126"

And my docker-compose.yaml looks like

version: "3"
services:
    ddtrace-test:
        build: .
        volumes:
          - ".:/app"
        links:
          - ddagent

    ddagent:
          image: datadog/docker-dd-agent
          environment:
              - DD_BIND_HOST=0.0.0.0
              - DD_API_KEY=<my key>
          ports:
              - "127.0.0.1:8126:8126"

So then I'm running the command docker-compose run --rm ddtrace-test python test.py, where test.py looks like

from ddtrace import tracer

@tracer.wrap('test', 'test')
def foo():
    print('running foo')

foo()

And when I run the command, I'm returned with

Starting service---reprocess_ddagent_1 ... done
foo
cannot send spans to localhost:8126: [Errno 99] Cannot assign requested address

I'm not sure what this error means. When I use my key and run from local instead of over a docker image, it works fine. What could be going wrong here?


Solution

  • Containers are about isolation so in container "localhost" means inside container so ddtrace-test cannot find ddagent inside his container. You have 2 ways to fix that:

    1. Put network_mode: host in ddtrace-test so he will bind to host's network interface, skipping network isolation
    2. Change ddtrace-test to use "ddagent" host instead of localhost as in docker-compose services can be accessed using theirs names