docker 中 运行 时无法从 python 连接到 influxdb
Cannot connect from python to influxdb when running in docker
标题不完全准确,欢迎提出建议!
您可以在此处找到完整的 MCVE:https://github.com/timstoop/20170614-python-docker-influxdb-problem
测试:
- docker-engine 17.05.0~ce-0~ubuntu-xenial
- docker-compose 版本 1.13.0,内部版本 1719ceb
因此 docker-compose 启动了一个 influxdb 和一个小型 python3 应用程序。该应用程序唯一做的就是尝试连接到 influxdb,运行 一个命令,如果失败,请等待 5 秒,然后重试。如果我 运行 docker-compose up
在此代码上,应用程序将永远不会连接,它会不断重试。这些是它输出的日志行:
app_1 | 2017-06-14 18:57:36.892955 ticker Trying InfluxDB connection...
app_1 | 2017-06-14 18:57:36.892977 ticker Influx host: 'influxdb'
app_1 | 2017-06-14 18:57:36.892984 ticker Influx port: 8086
app_1 | 2017-06-14 18:57:36.935112 ticker No InfluxDB connection yet. Waiting 5 seconds and retrying.
但是,如果我使用 docker exec -ti <container name> /bin/sh
在该特定容器中打开 shell,则以下工作正常:
/ # python3 -u
Python 3.6.1 (default, Jun 8 2017, 21:50:56)
[GCC 6.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from influxdb import InfluxDBClient
>>> ci = InfluxDBClient(host='influxdb')
>>> ci.get_list_database()
[{'name': '_internal'}]
>>>
我确定我忽略了一些愚蠢的东西,但我无法解释为什么 app.py
无法建立连接。因此,我无法修复该连接。如有任何建议,我们将不胜感激。
我的代码如下,供参考。
app.py:
#!/usr/bin/env python
from influxdb import InfluxDBClient
import datetime
import sys
import time
import os
import requests
def output(msg):
# Convenience function to always show a correct output
now = datetime.datetime.now()
print("%s ticker %s" % (now, msg))
if __name__ == '__main__':
# Gather our settings
influx_host = os.getenv('INFLUX_HOST', 'localhost')
influx_port = os.getenv('INFLUX_PORT', '8086')
influx_user = os.getenv('INFLUX_USER', 'root')
influx_pass = os.getenv('INFLUX_PASS', 'root')
# Create our connections
# Check to make sure we can create a connection
got_if_connection = False
while not got_if_connection:
output('Trying InfluxDB connection...')
output("Influx host: %s" % influx_host)
output("Influx port: %s" % influx_port)
influx_client = InfluxDBClient(host=influx_host, port=influx_port,
username=influx_user,
password=influx_pass)
try:
influx_client.get_list_database()
except requests.exceptions.ConnectionError:
output('No InfluxDB connection yet. Waiting 5 seconds and '+
'retrying.')
time.sleep(5)
else:
got_if_connection = True
Docker 文件:
FROM python:alpine3.6
MAINTAINER Tim Stoop <tim@kumina.nl>
# Copy the script in
COPY app.py /app.py
COPY requirements.txt /requirements.txt
# Install dependencies
RUN pip install -r /requirements.txt
CMD ["python3", "-u", "/app.py"]
docker-compose.yml:
version: '2'
services:
influxdb:
image: "influxdb:alpine"
ports:
- "8086:8086"
app:
build: .
links:
- influxdb
environment:
- INFLUX_HOST='influxdb'
如果您需要任何其他信息,请告诉我!
从环境部分的 docker-compose.yml
文件中删除引号。
environment:
- INFLUX_HOST=influxdb
标题不完全准确,欢迎提出建议!
您可以在此处找到完整的 MCVE:https://github.com/timstoop/20170614-python-docker-influxdb-problem
测试:
- docker-engine 17.05.0~ce-0~ubuntu-xenial
- docker-compose 版本 1.13.0,内部版本 1719ceb
因此 docker-compose 启动了一个 influxdb 和一个小型 python3 应用程序。该应用程序唯一做的就是尝试连接到 influxdb,运行 一个命令,如果失败,请等待 5 秒,然后重试。如果我 运行 docker-compose up
在此代码上,应用程序将永远不会连接,它会不断重试。这些是它输出的日志行:
app_1 | 2017-06-14 18:57:36.892955 ticker Trying InfluxDB connection...
app_1 | 2017-06-14 18:57:36.892977 ticker Influx host: 'influxdb'
app_1 | 2017-06-14 18:57:36.892984 ticker Influx port: 8086
app_1 | 2017-06-14 18:57:36.935112 ticker No InfluxDB connection yet. Waiting 5 seconds and retrying.
但是,如果我使用 docker exec -ti <container name> /bin/sh
在该特定容器中打开 shell,则以下工作正常:
/ # python3 -u
Python 3.6.1 (default, Jun 8 2017, 21:50:56)
[GCC 6.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from influxdb import InfluxDBClient
>>> ci = InfluxDBClient(host='influxdb')
>>> ci.get_list_database()
[{'name': '_internal'}]
>>>
我确定我忽略了一些愚蠢的东西,但我无法解释为什么 app.py
无法建立连接。因此,我无法修复该连接。如有任何建议,我们将不胜感激。
我的代码如下,供参考。
app.py:
#!/usr/bin/env python
from influxdb import InfluxDBClient
import datetime
import sys
import time
import os
import requests
def output(msg):
# Convenience function to always show a correct output
now = datetime.datetime.now()
print("%s ticker %s" % (now, msg))
if __name__ == '__main__':
# Gather our settings
influx_host = os.getenv('INFLUX_HOST', 'localhost')
influx_port = os.getenv('INFLUX_PORT', '8086')
influx_user = os.getenv('INFLUX_USER', 'root')
influx_pass = os.getenv('INFLUX_PASS', 'root')
# Create our connections
# Check to make sure we can create a connection
got_if_connection = False
while not got_if_connection:
output('Trying InfluxDB connection...')
output("Influx host: %s" % influx_host)
output("Influx port: %s" % influx_port)
influx_client = InfluxDBClient(host=influx_host, port=influx_port,
username=influx_user,
password=influx_pass)
try:
influx_client.get_list_database()
except requests.exceptions.ConnectionError:
output('No InfluxDB connection yet. Waiting 5 seconds and '+
'retrying.')
time.sleep(5)
else:
got_if_connection = True
Docker 文件:
FROM python:alpine3.6
MAINTAINER Tim Stoop <tim@kumina.nl>
# Copy the script in
COPY app.py /app.py
COPY requirements.txt /requirements.txt
# Install dependencies
RUN pip install -r /requirements.txt
CMD ["python3", "-u", "/app.py"]
docker-compose.yml:
version: '2'
services:
influxdb:
image: "influxdb:alpine"
ports:
- "8086:8086"
app:
build: .
links:
- influxdb
environment:
- INFLUX_HOST='influxdb'
如果您需要任何其他信息,请告诉我!
从环境部分的 docker-compose.yml
文件中删除引号。
environment:
- INFLUX_HOST=influxdb