0

I need to connect to existing db in my computer. When I create db service with postgis image, it creates a new database inside docker container. But I need to connect to my local one. Is it possible? docker-compose.yml :

  version: '3'
  services:
      build: ./
      command: bash -c "python manage.py makemigrations && python manage.py migrate && python manage.py collectstatic --noinput --i rest_framework && gunicorn orion-amr.wsgi:application --bind 0.0.0.0:8000 --workers=2"
      ports:
        - "${webport}:8000"
      env_file:
        - ./.env

settings.py :

DATABASES = {
    'default': {
        'ENGINE': 'django.contrib.gis.db.backends.postgis',
        'NAME': os.environ.get('pgdatabase'),
        'USER': os.environ.get('pguser'),
        'PASSWORD': os.environ.get('pgpassword'),
        'HOST': os.environ.get('pghostname'),
        'PORT': '5432',
    }
}

.env file:

pgport=5432
webport=8000
SECRET_KEY=z)mf(y#w7-_8y1)0(v*n@w@lzf)!0=g_rj5$%1w6g-t!7nbk05
pgdatabase=amr_or
pgpassword=root
pghostname=localhost
pguser=postgres
DEBUG=

Now I have this error:

web_1  | django.db.utils.OperationalError: could not connect to server: Connection refused
web_1  |        Is the server running on host "localhost" (127.0.0.1) and accepting
web_1  |        TCP/IP connections on port 5432?
web_1  | could not connect to server: Cannot assign requested address
web_1  |        Is the server running on host "localhost" (::1) and accepting
web_1  |        TCP/IP connections on port 5432
Aigerim Sadir
  • 353
  • 5
  • 18
  • 1
    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) – Kevin Kopf Jul 24 '20 at 16:22

1 Answers1

0

I really hope you did not expose your real SECRET_KEY to the rest of the internet. Don't forget to change it if you did.

If I remember correctly (based on a question you asked earlier this year), you are running Docker for Windows. Take a look at the docs over here, it says

The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Windows.

Hence, set pghostname to host.docker.internal instead of localhost.

Since you state, that you "need to connect to existing db in (your) computer", I will go ahead and assume you do this for development purposes, so this should suffice.

On production, however, you will need to run the database in a separate container and map the volumes.

Kevin Kopf
  • 13,327
  • 14
  • 49
  • 66
  • Ah, now I see this is a duplicate question... Please, try not to create duplicate questions or at least delete the old one if you're going to recreate it... – Kevin Kopf Jul 24 '20 at 16:22