1

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

1 Answers1

2

client = InfluxDBClient(host='localhost', port=8086)

When running in container, localhost means the container itself, not the host machine. So you have 2 solutions to choose:

  1. Change localhost to the ip of your host pc.
  2. When docker run, add --net=host to let container directly use the host network.
atline
  • 28,355
  • 16
  • 77
  • 113
  • `--net=host` completely disables Docker networking, and you'll have trouble doing things like connecting to other containers. [From inside of a Docker container, how do I connect to the localhost of the machine?](https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach) lists out several other alternatives. – David Maze Jun 16 '21 at 10:43