在 Python 中写入 InfluxDB 时服务未知

Service not known when writing to InfluxDB in Python

我将 Docker 与 InfluxDB 一起使用,并在框架中使用 Python。我想在框架内写入 InfluxDB,但我总是收到错误“名称或服务未知”并且不知道是什么问题。

我 link InfluxDB 容器到 docker compose 文件中的框架容器,如下所示:

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:

在框架内,我有一个只专注于写入数据库的脚本。因为我不想使用 url“localhost:8086”访问数据库,所以我使用 links 来简化操作并使用 url“influxdb”连接到数据库:8086”。这是我在该脚本中的代码:

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

每次我使用函数 insert_data 我都会得到错误 urllib3.exceptions.NewConnectionError: <urllib3.connection.HTTPConnection object at 0x7fac547d9d00>: Failed to establish a new connection: [Errno -2] Name or service not known

为什么我不能写入数据库?

我认为问题出在您的 docker-compose 文件中。首先,links 是一项遗留功能,因此我建议您改为使用 user-defined 网络。更多相关信息:https://docs.docker.com/compose/compose-file/compose-file-v3/#links

我创建了一个简约示例来演示该方法:

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:

注意附加的 networks 定义和 local 网络。此网络也是从容器中引用的。

还要确保根据 docker 图像的文档使用正确的环境变量初始化您的 influxdb:https://hub.docker.com/_/influxdb

然后通过 docker-compose:

在您的 framework 容器中测试 运行 一个 shell

docker-compose run --entrypoint sh framework

然后在容器中安装客户端:

pip install influxdb_client['ciso']

然后在 python shell - 仍在容器内 - 你可以验证连接:

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'}

最后但同样重要的是清理测试资源:

docker-compose down