Tengo un proyecto django con base de datos PostgreS. Necesito usar mi base de datos local en el contenedor docker. ¿Cómo compartir el host local de la máquina con el contenedor docker? Mi docker-compose.yml :
version: '3' services: web: build: ./ network_mode: "host" 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" - "${pgport}:${pgport}" env_file: - ./.envConfiguración de Django.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': os.environ.get('pgport'), } }yo añadí
listen_addresses = '*'al archivo postgresql.conf y debajo está mi archivo pg_hba.conf :
# TYPE DATABASE USER ADDRESS METHOD # IPv4 local connections: host all all 0.0.0.0/0 trust # IPv6 local connections: host all all ::/0 trust # Allow replication connections from localhost, by a user with the # replication privilege. host replication all 127.0.0.1/32 trust host replication all ::1/128 trustAhora tengo un error:
web_1 | django.db.utils.OperationalError: could not connect to server: Connection refused web_1 | Is the server running on host "127.0.0.1" and accepting web_1 | TCP/IP connections on port 5432? web_1 |¿Quién puede ayudarme? ¡Por favor!
parece que os.environ.get('pghostname') apunta a 127.0.0.1 donde 127.0.0.1 significa este contenedor, no Host DB. como el error
Is the server running on host "127.0.0.1" and accepting Para conectarse con la base de datos del host, tiene un DNS especial para mac y window host.docker.internal . El host de base de datos debe ser
'HOST': "host.docker.internal"Para conectarse con HOST DB, o si está en Linux, entonces debería ser
'HOST': "HOST_IP" Puede obtener la IP del host desde ifconfig .