title: Why won't my custom Dockerfile connect over the docker-compose network when other services will?
body: <h2>The problem</h2>
  <p>I am attempting to create a docker-compose file that will host three services. InfluxDB, Grafana, and a custom script in a customer Dockerfile that fills the database. I am having networking issues, and the custom script is not able to connect to the InfluxDB due to a <strong>connection refused error</strong> (shown below).</p>
  <h2>What is working so far</h2>
  <p>The interesting thing is, that when I remove the custom script service (called ads_agent) from my docker-compose file and either run that script from the localhost or even build and <strong>run that Dockerfile in its own container, it connects just fine</strong>.</p>
  <h2>What's the difference between the two</h2>
  <p>My script reads an environment variable called KTS_TELEMETRY_INFLUXDB_URL which is used for the InfluxDB client's URL to connect to. I can use &quot;http://localhost:8086&quot; for the URL when I run just from my command line, that works. I use my local machine's LAN IP address when I wrap the script in a Docker container because to it, localhost would be just the container. But nonetheless, this works just fine.</p>
  <p>From within my docker-compose, since all three services are on the same network, I'm using &quot;http://influxdb:8086&quot; since that host name should be bound to that service's network interface. And indeed it is, because <strong>Grafana is connecting just fine using that URL</strong>. Sadly, when I try this with the script, I get connection refused.</p>
  <h2>The error</h2>
  <pre><code>urllib3.exceptions.NewConnectionError: &lt;urllib3.connection.HTTPConnection object at 0x7f18c1fec970&gt;: Failed to establish a new connection: [Errno 111] Connection refused
  </code></pre>
  <h2>My code</h2>
  <p>This is my docker-compose.yaml</p>
  <pre><code>version: &quot;3&quot;
  services:
    influxdb:
      container_name: influxdb
      image: influxdb:2.0.9-alpine # influxdb:latest
      networks:
        - telemetry_network
      ports:
        - 8086:8086
      volumes:
        - influxdb-storage:/var/lib/influxdb2
      restart: always
      environment:
        - DOCKER_INFLUXDB_INIT_MODE=setup
        - DOCKER_INFLUXDB_INIT_USERNAME=$KTS_TELEMETRY_INFLUXDB_USERNAME
        - DOCKER_INFLUXDB_INIT_PASSWORD=$KTS_TELEMETRY_INFLUXDB_PASSWORD
        - DOCKER_INFLUXDB_INIT_ORG=$KTS_TELEMETRY_INFLUXDB_ORG
        - DOCKER_INFLUXDB_INIT_BUCKET=$KTS_TELEMETRY_INFLUXDB_BUCKET
        - DOCKER_INFLUXDB_INIT_RETENTION=$KTS_TELEMETRY_INFLUXDB_RETENTION
        - DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=$KTS_TELEMETRY_INFLUXDB_TOKEN
    grafana:
      container_name: grafana
      image: grafana/grafana:8.1.7 # grafana/grafana:latest
      networks:
        - telemetry_network
      ports:
        - 3000:3000
      volumes:
        - grafana-storage:/var/lib/grafana
      restart: always
      depends_on:
        - influxdb
    ads_agent:
      container_name: ads_agent
      build: ./ads_agent
      networks:
        - telemetry_network
      restart: always
      depends_on:
        - influxdb
      environment:
        - KTS_TELEMETRY_INFLUXDB_URL=http://influxdb:8086
        - KTS_TELEMETRY_INFLUXDB_TOKEN=$KTS_TELEMETRY_INFLUXDB_TOKEN
        - KTS_TELEMETRY_INFLUXDB_ORG=$KTS_TELEMETRY_INFLUXDB_ORG
        - KTS_TELEMETRY_INFLUXDB_BUCKET=$KTS_TELEMETRY_INFLUXDB_BUCKET
  
  networks:
    telemetry_network:
  
  volumes:
    influxdb-storage:
    grafana-storage:
  </code></pre>
  <p>This is my ads_agent/Dockerfile</p>
  <pre><code>FROM python:3.9
  COPY requirements.txt .
  RUN pip install --upgrade pip
  RUN pip install -r /requirements.txt
  COPY main.py .
  ENTRYPOINT /usr/local/bin/python3 /main.py
  </code></pre>
  <p>ads_agent/requirements.txt just has the influxdb-client and this is my ads/main.py</p>
  <pre><code>import os
  from influxdb_client import InfluxDBClient, Point, WritePrecision
  from influxdb_client.client.write_api import SYNCHRONOUS
  from datetime import datetime
  import random
  import time
  
  token = os.environ[&quot;KTS_TELEMETRY_INFLUXDB_TOKEN&quot;]
  org = os.environ[&quot;KTS_TELEMETRY_INFLUXDB_ORG&quot;]
  bucket = os.environ[&quot;KTS_TELEMETRY_INFLUXDB_BUCKET&quot;]
  url = os.environ[&quot;KTS_TELEMETRY_INFLUXDB_URL&quot;]
  
  client = InfluxDBClient(url=url, token=token)
  dbh = client.write_api(write_options=SYNCHRONOUS)
  
  while True:
      symbol_name = 'rand_num'
      value = random.random()
      timestamp = datetime.utcnow()
      print(timestamp, symbol_name, value)
      point = Point(&quot;mem&quot;) \
          .field(symbol_name, value) \
          .time(timestamp, WritePrecision.NS)
      dbh.write(bucket, org, point)
      time.sleep(1)
  </code></pre>