Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

like image 632
Matheus Campello Avatar asked Jan 10 '17 16:01

Matheus Campello


4 Answers

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
like image 192
tech Avatar answered Nov 14 '22 18:11

tech


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

like image 6
Mik Goldwyn Avatar answered Nov 14 '22 20:11

Mik Goldwyn


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.

like image 4
Matheus Campello Avatar answered Nov 14 '22 20:11

Matheus Campello


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..

like image 1
Octatron Avatar answered Nov 14 '22 19:11

Octatron