Why can't i connect to backend container (Database)

Hi there,

first of all I am new in the matter. :grin:

I have a docker-compse.yml to start all the backend stuff like InfuxDB Grafana and so on.

version: '3.7'

services:
  influxdb:
    image: influxdb:latest
    container_name: influxdb
    ports:
      - 8086:8086
    volumes:
      # Data persistency
      - ./vol/influxFol:/var/lib/influxdb:rw
      #- /mnt/mydisk/Persistence/influxFol:/var/lib/influxdb:rw
    environment: 
      - INFLUXDB_DB=metrics # Automatically initializes a database with the name of this environment variable
      - INFLUXDB_REPORTING_DISABLED=true
      - INFLUXDB_ADMIN_USER=influx_admin
      - INFLUXDB_ADMIN_PASSWORD=some_password
      - INFLUXDB_HTTP_LOG_ENABLED=false
      - INFLUXDB_DATA_QUERY_LOG_ENABLED=false
      # The name of a user to be created with no privileges. 
      - INFLUXDB_USER=collector # If INFLUXDB_DB is set, this user will be granted read and write permissions for that database
      - INFLUXDB_USER_PASSWORD=some_password. 
    restart: always
    deploy:
      resources:
        limits:
          memory: 500M

Now i start a python3 script to write datas into the influxDB by the official python InfluxDB-Client.
This works fine!

So i build a docker image and run this image in a docker-compose.yml. But when i do this, i can’t write into the database ? hm, why?

here is the Docker build file

FROM python:alpine3.9

COPY . /app

WORKDIR /app

RUN pip install --upgrade pip
RUN pip install -r requirements.txt

and the docker-compose.yml

version: '3.7'

services:
    writeToDB:
        image: writeToDB:latest
        container_name: writeToDB
        build: 
            context: .
            args: 
                - buildno=latest
        command: python3 runnerMain.py 
        volumes:
            - .:/app:rw
        restart: always
        deploy:
            resources:
              limits:
                memory: 200M
  
volumes:
    files: {}

I have tried to connect to the InfluxDB host by his IP 127.0.0.1 or as localhost, for all this i can’t write datas to the DB.

In the end of the day, you shouldnt publish databases ports but only the apps ports for security reasons (it’s okay while testing though :P)

“But if the database is not published, how my service are going to talk to it ?”
Simple : create a virtual network between your containers :

version: '3.7'
networks:
  privnet:

services:
  influxdb:
    image: influxdb:latest
    networks:
      - privnet
  writeToDB:
    image: writeToDB:latest
    networks:
      - privnet

I’m removing services options (like volumes, environnements, deploy
) for readability only, it’s still needed. (And you’ll want to add Grafana there too I guess)
Once this is done, your “writeToDB” python script can connect to the database using influxdb:8086. Note that here the “influxdb” is the service name defined in the docker-compose file.

thanks for your suggestions.
let me explain a little bit more

my folder looks like
root

  • docker-compose.yml "Influx and Grafana Stuff
  • folderPython
    • writeToDB.py
    • Dockerfile
    • docker-compose.yml "python compose file to write into DB

you can see i have two docker compose files. one for the backend and one for my writeToDB service.
i did this to update the writeToDB service as stand alone without to shutdown all services in the backend of the docker-compose file.

my aim is to have one backend in one main compose file with the backend services. and some independent single services like writeToDB. the single services will be upgraded several times but the backend only sometimes.

or is it better to use one docker-compose.yml with all services included? but how can i update on of the service without stop the other ones?

THX

I made it a single docker-compose file for simplicity only. It’s completly possible, to use several docker-compose files using the same network.
The 1st one declare the network, the other ones use it this way :

...
networks:
  privnet:
    external: true
...

Thanks for your help!

after many attempts I could find the error: If the service writeToDB wants to access the InfluxDB, it must not access to localhost/127.0.0.1, it must use the service itself here influxdb !