Communication between Python and InfluxDB in Docker

73 Views Asked by At

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, server represents my own application here:

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”, “http://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. (Sorry for the formating, Overflow doesn't make it easy)

Multi-Application Container, where Python and InfluxDB communicate. Unfortunately i get Errors when trying to establish the connection.

1

There are 1 best solutions below

1
On

http://localhost:8086 is expected to fail because influxdb is not running in the same container as your server. localhost in a container means "this container", just like localhost on your host means "this host".

http://influxdb:8086 is failing because you have explicitly placed your containers on different networks, and name resolution only works between containers on the same network. Get rid of your mynetwork network; you don't need it:

services:
  influxdb:
    image: influxdb:latest
    ports:
      - "8086:8086"
    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"
        - "http://localhost:8086/ping"
      interval: 10s
      timeout: 5s
      retries: 5
  server:
    build:
      context: .
    ports:
      - 8000:8000
    depends_on:
      influxdb:
        condition: service_healthy
volumes:
  influxdb: null