1

I want to run two docker containers - one hosting a Influx DB and one running a python script connecting to the Influx DB with the python client.

I made the dummy_script.py (see minimal example below):

from influxdb import InfluxDBClient
from requests.exceptions import ConnectionError

if __name__ == "__main__":
    
    # Connect to Influxdb
    try:
        client = InfluxDBClient(host='localhost', port=8086)
        print(client.ping())
    except ConnectionError as e:
        print("No Database Connection")

and turned it into a Docker container with the following Dockerfile:

FROM python:3

# set a directory for the app
WORKDIR /usr/src/app

# copy all the files to the container
COPY . .

# install dependencies
RUN pip install --no-cache-dir -r requirements.txt

# run program
CMD ["python", "./dummy_script.py"]

I want to spin both of them up with a docker-compose.yaml:

version: "3"
services:
  influxdb:
    image: influxdb
    ports:
      - "8086:8086"
    networks:
      - test_network
    volumes:
      - influxdb:/var/lib/influxdb
  dummy:
    image: dockerid/dummy_container
    links:
      - influxdb
    networks:
      - test_network
    volumes: 
      - dummy:/usr/src/app
networks:
  test_network:
volumes:
  influxdb:
  dummy:

The python script works fine when run from the command line. However, everytime I start it up as Docker container, my python client fails saying:

ConnectionRefusedError: [Errno 111] Connection refused

I have tried replacing localhost with influxdb in the client initialization as some github users suggested, but it only leads to a different error:

Failed to establish a new connection: [Errno -2] Name or service not known

I also tried waiting a bit until influxdb is up for sure or starting the dummy container manually a bit after the influx container, but still there's the error.

What am I missing or doing wrong?

Sebastian Dengler
  • 1,258
  • 13
  • 30
  • Try the container name from the docker-compose logs as the hostname. – Klaus D. Jun 29 '20 at 14:02
  • What's the networks name when you run `docker-compose up -d influxdb` ? – abestrad Jun 29 '20 at 14:22
  • How would I get the container name / the networks name? I'm not extremely proficient in docker yet, sorry for that. – Sebastian Dengler Jun 29 '20 at 14:31
  • @abestrad When I run your command it says something like: folder-name_influxdb_1 ... done – Sebastian Dengler Jun 29 '20 at 14:33
  • that's ok, the app’s network is given a name based on the “project name”. Now, try to see the DB container's name, `docker container ps` NAMES from the output. It might be the folders name + service name + _1 – abestrad Jun 29 '20 at 14:44
  • yes exactly it's the same as above: folder-name_influxdb_1 – Sebastian Dengler Jun 29 '20 at 14:49
  • Ok, that's the host you will be using to connect, `client = InfluxDBClient('foldersname_influxdb_1', 8086, 'root', 'root', 'example')` – abestrad Jun 29 '20 at 14:50
  • I rebuilt the docker image with the chages but it still says No Database Connection :/ – Sebastian Dengler Jun 29 '20 at 14:55
  • 1
    I've created an example here https://github.com/aestrad/Influxdb-py and the logs `influxdb_1 | ts=2020-06-29T15:12:12.561683Z lvl=info msg="Listening for signals" log_id=0NhA0YEW000 influxdb_1 | [httpd] 172.28.0.3 - root [29/Jun/2020:15:12:13 +0000] "GET /ping HTTP/1.1" 204 0 "-" "python-requests/2.24.0" e918a7bc-ba1a-11ea-8001-0242ac1c0002 126 dummy_1 | 1.8.0 dummy_1 | Connected` – abestrad Jun 29 '20 at 15:12
  • Thanks a lot It actually managed to connect now. I don't really understand why :D – Sebastian Dengler Jun 29 '20 at 15:29

0 Answers0