Create Dockerfile for python application to read InfluxDB

Hi all,

I am having a simple python script to fetch data from a table in InfluxDB installed in the local system. The deviceStatus.py script is as shown

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'])

This script runs without any error and prints the IP Address (Connection) from the table FactoryConfig.

Now I want to create a docker image out of it. I wrote a Dockerfile that looks like this

FROM python:3.10.0b2-buster

WORKDIR /usr/src/app

COPY deviceStatus.py .

RUN pip install influxdb

CMD ["python", "./deviceStatus.py"]

This file compiles and creates a docker image named devicestatus. Now when I try to run the image with
sudo docker run devicestatus

it shows me an error on line 8 and complains that it cannot establish a new connection: [Errno 111] Connection refused

File "/usr/src/app/./deviceStatus.py", line 8, in <module>
    results= client.query('SELECT (*) FROM "autogen"."FactoryConfig"')

I suppose it’s something to do with the port. I am not able to understand how can I expose the port if this is the problem. I need help regarding this issue.

Thanks in advance.

Cheers,
SD

It’s entirely possible that someone here may be able to help you with this
problem, however it’s really a Docker question more than an InfluxDB question,
so you might be better off asking on a Docker list / forum rather than here.

Antony.

1 Like

I have also posted it in the Docker forum.

I agree that this is more a docker/network problem.
I think that localhost within a container wil not resolve to the host machine out of the box.
Google for your problem, i am sure you will find some hints, maybe here:

1 Like

Hi @Franky1,

Yes, replacing localhost with IP solved the problem.