Docker Community Forums

Share and learn in the Docker community.

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

The query in your code example pretty much looks like a InfluxQL query, which is the old query langage for InfluxDB up to version 1.8.x. Within your Dockerfile, you install the latest InfluxDB version, which is a 2.0+ version that requires Flux queries instead.

If you update your local InfluxDB instance (pip update influxdb), you should experience the same problem. Either make sure to install an older version of InfluxDB within your Dockerfile, or preferably migrate your code to the latest InfluxDB version.

1 Like

Hi @meyay,

In the Dockerfile, I install the InfluxDB python library. Is there a separate library for 1.8 as well? This python script with the same library works fine when run on the bare Linux OS. The problem arises only when I dockerize it.

I am no python guy.

I just happen to know the situation with the recent influxdb version change and the incompatibility with the query language they require.

From my perspective this apears to be rather a problem of using consitent versions of the server component and the matching client sdk, than a containerization problem.

1 Like

I understand. But at the same time, I am surprised to see the code working correctly outside the container and breaks when I dockerize it.

Change localhost to the ip of host pc.

Problem solved!

So your solution is to use the host instance of influxdb, instead of using the instance running in the container?

To put it in other words: you just care to containerize your self created influxdb client, but want the actual influxdb server to be run outside your container. At least this is what you did and describe as solution.

I am having the InfluxDB running in another container. This was just an added feature that I wanted to include without disturbing the running InfluxDB container.

Also, including InfluxDB in this container was never the plan!

Ah okay. Now the hole situation makes more sense to me :slight_smile:
I can see that my previous reponses must have been very misleading from your perpective. Sorry for that!

My assumption was that RUN pip install influxdb installs the server and your python code is the client. Though, it didn’t appear to me that pip install fluxdb would just install the sdk.

ya, no worries.
So pip install influxdb is the python command to install the python influxdb client library.
In the context of image creation using Dockerfile, I have a new issue.

image

You can see that I have devicestatus:version1 which I create using Dockerfile and python:3.6.13 is the dependency. But the Dockerfile also generates 3 : images. What are these and why are they generated? I don’t want these images!

Those appear to be intermediate images, caused each by an instruction in your Dockerfile. Use docker build --force-rm ... to remove intermediate images regardless if the build succeeds or fails. If you just want to remove intermediate images from succeesful builds use --rm instead of --force-rm.

Note: if you build an image:tag again, the previous image:tag gets “untagged” und becomes a <none> tag.

1 Like

Hi @meyay,

Thanks a ton for your support! :slight_smile:

Welcome! And sorry again for the confusion in the beginning.