Communication between Python and InfluxDB in Docker

Hi,
I am trying to create an application that let’s me write data to an InfluxDB within Docker. When I execute the code locally and run Influx on Docker everything works fine.
I am now trying to put both into its own container with a compose.yaml that looks like this:

version: "3"
services:
   influxdb:
   image: influxdb:latest
   ports:
      - "8086:8086"
   networks: 
      - mynetwork
   volumes:
      - influxdb:/var/lib/influxdb
   environment:
      - DOCKER_INFLUXDB_INIT_MODE=setup
      - DOCKER_INFLUXDB_INIT_USERNAME=root
      - DOCKER_INFLUXDB_INIT_PASSWORD=secret_password
      - DOCKER_INFLUXDB_INIT_ORG=my_org
      - DOCKER_INFLUXDB_INIT_BUCKET=my_bucket
      - DOCKER_INFLUXDB_INIT_RETENTION=1w
      - DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=secret_token
   healthcheck:
      test: ["CMD", "curl", "-f", "localhost:8086/ping"]
      interval: 10s
      timeout: 5s
      retries: 5
server:
   build:
      context: .
   ports:
      - 8000:8000
   depends_on:
      influxdb:
         condition: service_healthy
networks:
   mynetwork:
volumes:
   influxdb:

The problem occurs when trying to connect to the Client:
write_client = InfluxDBClient(url=url, token=token, org=org)

  • When i use this URL: “http://localhost:8086”, I get the error: Failed to establish a new connection: [Errno 111] Connection refused
  • When i use this URL: “http://influxdb:8086”, I get the error: Failed to resolve ‘influxdb’ ([Errno -3] Temporary failure in name resolution

I have run all out of ideas I would greatly appreciate, if someone had some insights into this.

(the localhost at the health_check is a link as well, the guidelines just don’t let me post it, sorry for the inconvenience)

The problem was that I specified a network for InfluxDB, that I didn’t use for my server, so they couldn’t communicate. I solved the problem by deleting all lines related to the network:)