0

So, I face a really strange bug. I have a dockerized application which is already running in a live server. what I really need to do now is to make ti work locally and the only thing I am getting is this error: django.db.utils.OperationalError: could not connect to server: Connection refused. Is the server running on host "127.0.0.1" and accepting TCP/IP connections on port 5432?

This is my docker-compose.yml file:

version: "3"
services:
#
backend:
  build:
    context: diabetesmelitusbackend
     args:
      - DATABASE_ENGINE=${DATABASE_ENGINE}
      - DATABASE_NAME=${DATABASE_NAME}
      - DATABASE_HOST=${DATABASE_HOST}
      - DATABASE_USER=${DATABASE_USER}
      - DATABASE_PASSWORD=${DATABASE_PASSWORD}
      - DATABASE_PORT=${DATABASE_PORT}
      - DJANGO_SUPERUSER_USERNAME=${DJANGO_SUPERUSER_USERNAME}
      - DJANGO_SUPERUSER_EMAIL=${DJANGO_SUPERUSER_EMAIL}
      - DJANGO_SUPERUSER_PASSWORD=${DJANGO_SUPERUSER_PASSWORD}
  image: backend
  ports:
    - "9000:9000"
  extra_hosts:
    - "host.docker.internal:127.0.0.1"

and this is my .env file

DATABASE_ENGINE=django.db.backends.postgresql
DATABASE_NAME=db_name
DATABASE_HOST=db
DATABASE_USER=db_user
DATABASE_PASSWORD=1234
DATABASE_PORT=5432 
DJANGO_SUPERUSER_USERNAME=admin
DJANGO_SUPERUSER_EMAIL=admin@dm.gr 
DJANGO_SUPERUSER_PASSWORD=1234

Build is working fine but the up in localhost is returning the error about the connection. Any ideas?

On the .env file, I have tried to replace DATABASE_HOST with localhost, 127.0.0.1 and still nothing. Also I have allowed listen_addresses = '*' and I have added the localhost to pg_hba.conf file

  • is postgres on localhost running fine? please check logs of postgres. – charybr Nov 09 '22 at 15:16
  • What's running in the container, django or django and postgres? If you want to access postgres running on your localhost from the container, the database host should be: host.docker.internal – Robert Moskal Nov 09 '22 at 15:19
  • @RobertMoskal - ... not if set to 127.0.0.1, it should be the "real" IP or that of the docker bridge, given that postgres listens on all interfaces (and not localhost alone) – JeffRSon Nov 09 '22 at 15:40
  • @JeffRSon True enough. I misread the file. So the problem is accessing the actual host from the docker container which is has an answer. – Robert Moskal Nov 09 '22 at 15:48
  • Does this answer your question? [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) – Robert Moskal Nov 09 '22 at 15:48
  • I voted to close this, the answer is here: https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach – Robert Moskal Nov 09 '22 at 15:49
  • @RobertMoskal unfortunately no. If I try the accepted answer and use this host.docker.internal:host-gateway, then I got this error: invalid IP address in add-host: "host-gateway" – marissalianam Nov 09 '22 at 15:59
  • remove extra_hosts: - "host.docker.internal:127.0.0.1" from your compose file. The answer to your question is def in that post. You are trying to access localhost from within the container – Robert Moskal Nov 09 '22 at 16:10
  • @RobertMoskal I am not sure what else to try. I tried 1) to remove completely extra hosts from docker-compose.yml -> connection refused 2) to use host.docker.internal:host-gateway -> invalid IP address in add-host: "host-gateway" 3) to use host.docker.internal:127.0.0.1 -> connection refused 4) only 127.0.0.1 -> bad format for add-host: "127.0.0.1" 5) only host.docker.internal -> bad format for add-host: "127.0.0.1" I don;t find anythng else to that post – marissalianam Nov 09 '22 at 20:23

1 Answers1

0

So finally I got what was wrong (at least on Ubuntu 20) On the .env file, on the DATABASE_HOST I was putting the localhost (or 127.0.0.1) which indeed is the localhost of the container. In order to make it connect to my machine's localhost, I had to put this IP 172.17.0.1.

The extra_hosts of the docker-compose.yml file didn't really matter.