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?