我正在尝试创建一个应用程序,让我可以将数据写入 Docker 中的 InfluxDB。当我在本地执行代码并在 Docker 上运行 Influx 时,一切正常。 我现在尝试将两者放入自己的容器中,其中的 compose.yaml 如下所示,服务器在这里代表我自己的应用程序:
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:
尝试连接客户端时出现问题:
write_client = InfluxDBClient(url=url, token=token, org=org)
当我使用此 URL:“http://localhost:8086”时,我收到错误:
Failed to establish a new connection: [Errno 111] Connection refused
当我使用此 URL:“http://influxdb:8086”时,我收到错误:
Failed to resolve ‘influxdb’ ([Errno -3] Temporary failure in name resolution
如果有人对此有一些见解,我将不胜感激。 (抱歉格式化,溢出并不容易)
多应用程序容器,Python 和 InfluxDB 进行通信。 不幸的是,我在尝试建立连接时遇到错误。
http://localhost:8086
预计会失败,因为 influxdb 没有在与服务器相同的容器中运行。容器中的 localhost
表示“此容器”,就像主机上的 localhost
表示“此主机”一样。
http://influxdb:8086
失败是因为您已明确将容器放置在不同的网络上,并且名称解析仅适用于同一网络上的容器之间。摆脱你的mynetwork
网络;你不需要它:
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