I want to set up the datadog agent to monitor my python application running inside a docker container. I have created the docker image tagged as datadog_app:v_1. Below is my docker file:
FROM ubuntu:18.04
WORKDIR /app
RUN apt-get update --no-install-recommends
RUN apt-get install -y build-essential python3.6 python3.6-dev python3-pip python3-setuptools --no-install-recommends
RUN pip3 install Cython
RUN pip3 install ddtrace
RUN apt-get install -y curl
ADD ./datadog_app.py /app/handler.py
RUN DD_AGENT_MAJOR_VERSION=7 DD_API_KEY=my_api_key DD_SITE="datadoghq.com" bash -c "$(curl -L https://s3.amazonaws.com/dd-agent/scripts/install_script.sh)"
CMD ddtrace-run python3 handler.py
Below is the code for the datadog_app.py file:
from ddtrace import tracer
import time
@tracer.wrap(service="addition")
def handler():
time.sleep(2)
print(1+2)
handler()
Now, I am trying to run a docker container from the "datadog_app:v_1: image in interactive and making sure to expose the port 8126 way by using the following command:
docker run -it -p 8126:8126 datadog_app:v_1
I am getting the following error:
ERROR:ddtrace.internal.writer:failed to send traces to Datadog Agent at http://localhost:8126
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/tenacity/__init__.py", line 407, in __call__
result = fn(*args, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/ddtrace/internal/writer.py", line 356, in _send_payload
response = self._put(payload, headers)
File "/usr/local/lib/python3.6/dist-packages/ddtrace/internal/writer.py", line 332, in _put
conn.request("PUT", self._endpoint, data, headers)
File "/usr/lib/python3.6/http/client.py", line 1281, in request
self._send_request(method, url, body, headers, encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1327, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1276, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File "/usr/lib/python3.6/http/client.py", line 1042, in _send_output
self.send(msg)
File "/usr/lib/python3.6/http/client.py", line 980, in send
self.connect()
File "/usr/lib/python3.6/http/client.py", line 952, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/usr/lib/python3.6/socket.py", line 724, in create_connection
raise err
File "/usr/lib/python3.6/socket.py", line 713, in create_connection
sock.connect(sa)
OSError: [Errno 99] Cannot assign requested address
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/ddtrace/internal/writer.py", line 458, in flush_queue
self._retry_upload(self._send_payload, encoded, n_traces)
File "/usr/local/lib/python3.6/dist-packages/tenacity/__init__.py", line 404, in __call__
do = self.iter(retry_state=retry_state)
File "/usr/local/lib/python3.6/dist-packages/tenacity/__init__.py", line 361, in iter
raise retry_exc from fut.exception()
tenacity.RetryError: RetryError[<Future at 0x7f32383f0a58 state=finished raised OSError>]
I have also tried running the following command:
docker run -it --expose=8126 datadog_app:v_1
But I am getting the same error. My goal is to run the datadog-agent inside the same container that contains my python application. Can someone explain what is wrong with my code or my run commands?