Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

509
Visualizações
How to connect a django app to a dockerized postgres db, both from a dockerized django and non-dockerized django using same DATABASE_HOST

I have a postgres container, docker-compose.yml:

services:
  db:
    container_name: db
    expose:
      - "5432"
    ports:
      - 5432:5432
    volumes:
      - postgres_data:/var/lib/postgresql/data/

And a django project with settings.py:

DATABASES = {
    'default': {
        'HOST': os.environ.get('POSTGRES_HOST', '127.0.0.1')
        # etc
    }
}

.env

POSTGRES_HOST_DJANGO=db

When I run my django app locally with manage.py runserver 0.0.0.0:8000 it connects fine, using the default POSTGRES_HOST=127.0.0.1, because .env isn't loaded.

I also run my django app in a container sometimes:

docker-compose.yml:

   web:
    #restart: unless-stopped
    build: .
    env_file: .env
    command: bash -c "cd /app/src/ && python manage.py runserver 0.0.0.0:8000
    volumes: 
      - .:/app
    ports:
      - 8000:8000
    links: 
      - db:db

However it uses the .env file and connects with POSTGRES_HOST=db

If I try to connect the locally run django app with POSTGRES_HOST=db it fails:

django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known

And if I try to run the django app in a container with POSTGRES_HOST=127.0.0.1, it fails in the same way.

How can I get them to use the same POSTGRES_HOST?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The problem seems to be in the network config. I don't see one.

The default behavior of docker-compose is that it creates a network for every compose file - by default the name is the folder name with '_default'.

Django App is in a different network and Postgres is in a different network

If your Django app and your Postgres containers are in different docker-compose files, using container names to resolve hosts will not work(by default, can be done with a custom network config) as they are in two different networks.

As you have done a port binding, you can directly access Postgres by giving host machine's private ip and port 5432 in the container, this way communication is happening through the host network.

If you find a need to make the containers talk to each other directly, make sure they are on the same docker network

over 4 years ago · Santiago Trujillo Relatório

0

I figured out how to do it. It wasn't getting them to use the same variable, it was to get them to read different variables based on how it was run. So:

from docker-compose.yml

   web:
    build: .
    command: bash -c "cd /app/src/ && python manage.py runserver 0.0.0.0:8000
    env_file: .env
    environment:
      POSTGRES_HOST: db  # takes precendent over .env file

And in .env:

POSTGRES_HOST=127.0.0.1

Now, when I run locally, with ./manage.py runserver, it uses the .env file and connects to the db container properly at 127.0.0.1:5342

But if I run docker-compose up web, even though it also read the .env file, the environment variable provided in the compose file takes precedent and it uses POSTGRES_HOST: db and connects to the db container as well!

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda