问题描述
我有一个简单的 Python 脚本来从安装在本地系统中的 InfluxDB 中的表中获取数据。 deviceStatus.py 脚本如图所示
import time
import sys
import influxdb
from influxdb import InfluxDBClient
client = InfluxDBClient(host='localhost',port=8086)
client.switch_database('deviceConfiguration')
results = client.query('SELECT (*) FROM "autogen"."FactoryConfig"')
points = results.get_points()
for point in points:
print(point['Connection'])
此脚本运行时没有任何错误,并从 FactoryConfig 表中打印 IP 地址(连接)。
现在我想用它创建一个 docker 镜像。我写了一个看起来像这样的 Dockerfile
FROM python:3.10.0b2-buster
workdir /usr/src/app
copY deviceStatus.py .
RUN pip install influxdb
CMD ["python","./deviceStatus.py"]
这个文件编译并创建了一个名为 devicestatus 的 docker 镜像。现在当我尝试使用
运行图像时sudo docker run devicestatus
它在第 8 行向我显示错误并抱怨它无法建立新连接:[Errno 111] 连接被拒绝
File "/usr/src/app/./deviceStatus.py",line 8,in <module>
results= client.query('SELECT (*) FROM "autogen"."FactoryConfig"')
我想这与端口有关。如果这是问题,我无法理解如何公开端口。我需要有关此问题的帮助。
提前致谢。
干杯, 标清
解决方法
client = InfluxDBClient(host='localhost',port=8086)
在容器中运行时,localhost
表示容器本身,而不是主机。因此,您有两种解决方案可供选择:
- 将
localhost
更改为您的主机 pc 的 ip。 - docker运行时,添加
--net=host
让容器直接使用宿主机网络。