Docker-compose with django could not translate host name "db" to address: Name or service not known
Asked Answered
A

7

38

I have currently a system built with docker-compose, it creates a Django application.

Up until now I've used a database inside a container (postgresql) in my testing build. Now I've changed the database from this container to an RDS instance in AWS.

Using Pg_dump I have recreated the database inside RDS and changed the settings.py, everything was supposedly normal. I have accessed the data from the database inside my webapp without any problems.

Everything was ok until I had to make a migration. Without the database container the Django container gives me this message:

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

My Docker-compose.yml file before the changes:

 version: '2'

    services:
      db:
        image: postgres:9.5
        restart: always
        environment:
          POSTGRES_USER: testing
          POSTGRES_PASSWORD: tests
          POSTGRES_DB: test
        volumes:
          - /dbdata:/var/lib/postgresql/data
      django:
        build: ./django
        command: gunicorn contactto.wsgi:application -b 0.0.0.0:8000
        restart: always
        volumes:
          - ./django:/usr/src/app
          - ./django/static:/usr/src/app/contactto/static
        ports:
          - "8000:8000"
        depends_on:
          - db

Now after the changes:

    version: '2'

    services:
      django:
        build: ./django
        command: gunicorn contactto.wsgi:application -b 0.0.0.0:8000
        restart: always
        volumes:
          - ./django:/usr/src/app
          - ./django/static:/usr/src/app/contactto/static
        ports:
          - "8000:8000"

And the DATABASES from settings.py . Before:

DATABASES = {
        'default': {
            'ENGINE': 'tenant_schemas.postgresql_backend',
            'NAME': 'testing',
            'USER': 'test',
            'PASSWORD': 'test',
            'HOST': 'db',
            'PORT': '5432',
        }
    }

After:

DATABASES = {
        'default': {
            'ENGINE': 'tenant_schemas.postgresql_backend',
            'NAME': 'testing',
            'USER': 'test',
            'PASSWORD': 'test',
            'HOST': 'xxx.rds.amazonaws.com',
            'PORT': '5432',
        }
    }

The weird thing is, I can use the aws database inside my app... I can create users and do things inside the database and the changes appear. Now in the CLI I can't even use manage.py shell without the message.

I am completely lost.

Absurdity answered 10/1, 2017 at 16:13 Comment(1)
The answer can be found at this https://mcmap.net/q/411335/-why-this-error-docker-operational-error-django-projectErgosterol
S
17

Add network, link and depends_on configuration in docker compose file.

example:

  services:
      db:
          build: .
          container_name: db
          networks:
              - djangonetwork
      web:
          build: .
          depends_on:
             - db
          links:
             - db:db
          networks:
             - djangonetwork

  networks:
      djangonetwork:
          driver: bridge
Schweiz answered 23/5, 2020 at 12:14 Comment(2)
This greatly helped me get past the docs.docker Quickstart, which is sans networks. Two things I tripped up on with this is the networks being at the same level as services and adding the networks to both the web and db configurations.Fudge
This got me past a problem with my compose file. Namely I did not have the networks field for my web defined.Carlenecarleton
O
12

To the others experiencing this.

The following command (which removes all unused containers, networks, images, and optionally, volumes) solve my problem:

docker system prune

See docker document for more information

Ornithic answered 10/4, 2020 at 14:33 Comment(3)
It would be much better if this answer explained why taking this action fixes the problem.Derisive
Why does this work? Is it a magic spell?Purposive
Usually such an error appears because docker volumes overreach the limit of how much size can be stored. If the data generated by all volumes reach a certain threshold (disk space), containers stop to function properly (especially when a --build operation is performed). docker system prune is a way of freeing memory, thus allowing to container to allocate memory and have enough space to function properly. This is why this command works like charm.Sagittarius
A
4

Answering my question, this was a stupid one...

My manage.py was selecting only the base.py settings file, and was not taking into account the staging.py settings file. So it was breaking in the CLI and was not breaking inside the app.

Absurdity answered 10/1, 2017 at 16:59 Comment(4)
Does that mean you didn't have the settings file configured correctly?Bushore
Can someone elaborate how he fixed that issue? I'm also getting this error it's being two days and I didn't find a solution :(Roam
I had this same issue while trying to run tests in Github Actions. The key insight is that your django app is starting up and attempting to connect to postgres. Somewhere in your settings, or in inherited settings at the time of run, the database name / port config is incorrect. If you think you know where you're setting it, and you change it, and still see 'db' then it is being overridden somewhere or you aren't actually working with the branch you think you are.Sexagesimal
@JohnMee No as he said his manage.py was pointing toward wrong settings file.See in application development a good practice is making several settings file for diffrent environments which will make running program smother , safer and faster in each environment.Annabelleannabergite
E
1

In the "django" part of your docker-compose.yml under "depends_on" section try adding links: - db:db after it, or even replace the depends_on: db with that.

I'll bet if you typed in docker logs (container name) you'd see it's obviously having trouble working out where "db" points to.

I think they're looking to end support for links in docker-compose in new iterations at least I read that somewhere..

Evanescent answered 7/8, 2018 at 2:4 Comment(0)
P
1

FWIW, for anyone using Podman, this issue could be due to the containers not being able to find each other within the network. This post helped me solve the problem https://github.com/containers/podman/issues/9292.

Essentially, you also need to make sure that the network's dns_enable is set to true.

Porosity answered 22/6, 2023 at 18:39 Comment(0)
N
0

If you use docker (compose), you can try:

  1. Terminal: docker compose down --remove-orphans
  2. Terminal: docker compose up -d

The problem seems to occure because of stale containers/networks which are interfering with the DNS resolution. By using docker system prune or a simple down and up you remove/restart those containers. By using --remove-orphans you also take care of containers not started by the docker-compose.yml

Nisbet answered 28/2, 2023 at 23:13 Comment(0)
S
-2

click on this link to get your answer or add command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000" in docker-compose.yaml.I hope this will work for you. It worked for me

Stacey answered 14/3, 2021 at 2:23 Comment(1)
Why did it work for you and why should it work for them?Purposive

© 2022 - 2024 — McMap. All rights reserved.