Home > OS >  Service not known when writing to InfluxDB in Python
Service not known when writing to InfluxDB in Python

Time:03-14

I'm using Docker with InfluxDB and Python in a framework. I want to write to InfluxDB inside the framework but I always get the error "Name or service not known" and have no idea what is the problem.

I link the InfluxDB container to the framework container in the docker compose file like so:

version: '3'
  services:
    influxdb:
      image: influxdb
      container_name: influxdb
      restart: always
      ports:
        - 8086:8086
      volumes:
        - influxdb_data:/var/lib/influxdb

    framework:
      image: framework
      build: framework
      volumes:
        - framework:/tmp/framework_data
      links:
        - influxdb
      depends_on:
        - influxdb

 volumes:      
    framework:
      driver: local
    influxdb_data:

Inside the framework I have a script that only focuses on writing to the database. Because I don't want to access the database with the url "localhost:8086", I am using links to make it easier and connect to the database with the url "influxdb:8086". This is my code in that script:

from influxdb_client import InfluxDBClient, Point
from influxdb_client.client.write_api import SYNCHRONOUS, WritePrecision

bucket = "bucket"
token = "token"

def insert_data(message):  
    client = InfluxDBClient(url="http://influxdb:8086", token=token, org=org)
    write_api = client.write_api(write_options=SYNCHRONOUS)

    point = Point("mem") \
        .tag("sensor", message["sensor"]) \
        .tag("metric", message["type"]) \
        .field("true_value", float(message["true_value"])) \
        .field("value", float(message["value"])) \
        .field("failure", message["failure"]) \
        .field("failure_type", message["failure_type"]) \
        .time(datetime.datetime.now(), WritePrecision.NS)

    write_api.write(bucket, org, point)  #the error seams to happen here

Everytime I use the function insert_data I get the error urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fac547d9d00>: Failed to establish a new connection: [Errno -2] Name or service not known.

Why can't I write into the database?

CodePudding user response:

I think the problem resides in your docker-compose file. First of all links is a legacy feature so I'd recommend you to use user-defined networks instead. More on that here: https://docs.docker.com/compose/compose-file/compose-file-v3/#links

I've created a minimalistic example to demonstrate the approach:

version: '3'
services:
  influxdb:
    image: influxdb
    container_name: influxdb
    restart: always
    environment: # manage the secrets the best way you can!!! the below are only for demonstration purposes...
      - DOCKER_INFLUXDB_INIT_USERNAME=admin
      - DOCKER_INFLUXDB_INIT_PASSWORD=secret
      - DOCKER_INFLUXDB_INIT_ORG=my-org
      - DOCKER_INFLUXDB_INIT_BUCKET=my-bucket
      - DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=secret-token
    networks: 
      - local

  framework:
    image: python:3.10.2
    depends_on:
      - influxdb
    networks:
      - local

networks:
  local:

Notice the additional networks definition and the local network. Also this network is referenced from the containers.

Also make sure to initialize your influxdb with the right enviroment variables according to the docker image's documentation: https://hub.docker.com/_/influxdb

Then to test it just run a shell in your framework container via docker-compose:

docker-compose run --entrypoint sh framework

and then in the container install the client:

pip install influxdb_client['ciso']

Then in a python shell - still inside the container - you can verify the connection:

from influxdb_client import InfluxDBClient
client = InfluxDBClient(url="http://influxdb:8086", token="secret-token", org="my-org")  # the token and the org values are coming from the container's docker-compose environment definitions
client.health()
# {'checks': [],
# 'commit': '657e1839de',
# 'message': 'ready for queries and writes',
# 'name': 'influxdb',
# 'status': 'pass',
# 'version': '2.1.1'}

Last but not least to clean up the test resources do:

docker-compose down

  • Related