Django docker - could not translate host name "db" to address: nodename nor servname provided, or not known

docker django
django development with docker
django docker localhost
setup docker with django
error: database is uninitialized and superuser password is not specified.
django docker deployment
docker-compose django example
django docker template

I'm relatively new to Django and Docker and am following a tutorial to build a mini application. However, I'm getting stuck with the following error:

django.db.utils.OperationalError: could not translate host name "db" to address: nodename nor servname provided, or not known

My docker-compose file looks as follows:

 version: '3'
 services:
  db:
     image: 'postgres'
     ports:
       - '5432'
  core:
    build:
      context: .
      dockerfile: Dockerfile
    command: python3 manage.py runserver 0.0.0.0:8000
    ports:
      - '8000:8000'
    volumes:
      - .:/code
    depends_on:
      - db
    links:
      - db:db

My settings.py file contains the database:

DATABASES = {
'default': {
    'ENGINE': 'django.db.backends.postgresql',
    'NAME': 'postgres',
    'USER': 'postgres',
    'HOST': 'db',
    'PORT': 5432,
}
}

I've seen the post here and here however both have not fixed the issue.

Would appreciate some guidance. Thanks.

So you are trying to reach the db which is running in one container from another container? If yes - the following could potentially help, at least it helped me when I had similar issues.

Try to define networks config in addition to links in your compose file, create a network with some name and define it in both services. Like described here, as the docks on links config recommend to do that.

Something like this for your case:

version: '3'
 services:
  db:
     image: 'postgres'
     ports:
       - '5432'
     networks:
      some_network:
  core:
    build:
      context: .
      dockerfile: Dockerfile
    command: python3 manage.py runserver 0.0.0.0:8000
    ports:
      - '8000:8000'
    volumes:
      - .:/code
    depends_on:
      - db
    links:
      - db:db
    networks:
      some_network:
  networks:
   some_network:

It helped me to resolve the host name to connect to the db.

Docker-compose with django could not translate host name "db" to , Docker-compose with django could not translate host name "db" to address: Name or service not known. I have currently a system built with docker-compose, it creates a Django application. Using Pg_dump I have recreated the database inside RDS and changed the settings.py, everything was supposedly normal. psycopg2.OperationalError: could not translate host name "toto.postgres.database.azure.com" to address: Temporary failure in name resolution with the command : docker run --rm -it -p 8000:8000 admin_marque When i try to open localhost:8000 in a browser. The main goal is to deploy it on azure

I had to use docker-compose up to make sure my db container was running.

Discussion of Django, Docker, and PostgreSQL Tutorial - DEV, OperationalError: could not translate host name "db" to address: Name or service not known . How can I fix this? Favorite heart outline button  django.db.utils.OperationalError: could not translate host name “db” to address: Name or service not known It appears the app container can’t find the database container when I run the test in PyCharm.

I've been searching for several days for the solution to this issue. Here's what I did:

1 - I copied the postgresql.conf.sample file from my postgres container to my project folder you must get in to cd usr/share/postgresql with docker exec -it yourcontainer bash

2 - I changed its name to postgresql.conf

3 - I modified these variables in postgresql.conf

    listen_addresses = '*'
    port = 5432             
    max_connections = 100           
    superuser_reserved_connections = 3
    unix_socket_directories = '/tmp'

4 - I added it to my Dockerfile

COPY postgresql.conf      /tmp/postgresql.conf

5 - settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'test',
        'USER': 'testing',
        'PASSWORD': 'tests',
        'HOST': 'localhost',
        'PORT': 5432,
    }
}

6 - I did docker-compose up -build again

could not translate host name "postgres" to address: Name or , Pulling docker image postgres:9-alpine . RAILS_ENV=test bundle exec rake db:create could not translate host name "postgres" to address: Name or service  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more could not translate host name “db” to address when running Django test in Docker through PyCharm

I came across this issue recently, i solved it by adding postgres environment variables to db in my docker-compose.yml file like so

version: '3'
 services:
  db:
     image: 'postgres'
     ports:
       - '5432'
     environment:
            - POSTGRES_DB=booksdb
            - POSTGRES_USER=postgres
            - POSTGRES_PASSWORD=postgres
  core:
    build:
      context: .
      dockerfile: Dockerfile
    command: python3 manage.py runserver 0.0.0.0:8000
    ports:
      - '8000:8000'
    volumes:
      - .:/code
    depends_on:
      - db
    links:
      - db:db

Docker-compose django and postgresql - Compose, django.db.utils.OperationalError: could not translate host name “db2” to address: Name or service not known. And all other builds stop during  psycopg2.OperationalError: could not translate host name "db" to address: Name or service not known. The db container is there though, and docker logs db return info for it. Here is the code the VSCode is running when I try to debug (I did not set any of these settings):

Issue with postgre in docker-compose, django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known The command "docker-compose  Dismiss Join GitHub today. GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.

could not translate host name "postgres" to address: Name does not , could not translate host name "postgres" to address: Name does not resolve I am using docker swarm to set env django database settings :. Quickstart: Compose and Django Estimated reading time: 7 minutes This quick-start guide demonstrates how to use Docker Compose to set up and run a simple Django/PostgreSQL app. Before starting, install Compose. Define the project components. For this project, you need to create a Dockerfile, a Python dependencies file, and a docker-compose.yml

django.db.utils.OperationalError: could not translate host name "db , Hi @Sebastian Cheung, does it work correctly outside of PyCharm? Could you please share your docker-compose.yml ? – django.db.utils.OperationalError: could not translate host name "db" to address: Name or service not known. any idea why this might be? running on a linux server with: docker version == 18.03.1-ce django version == 1.11.1

Comments
  • Can you keep postgres container running and restart only django container and tell me if you still getting the same error ?
  • Thanks for your comment Mostafa. Apologies I'm still quite new to Docker so may need a hand doing that. I've stopped docker. How can I just get the Postgres container running? Thanks.
  • docker-compose restart core this will restart the application without the database so make sure that the database is running first then execute the command
  • Thanks. Still have the issue unfortunately.
  • Let us continue this discussion in chat.