1

I'm trying to Dockerize a Django + Postgres project following this tutorial: https://docs.docker.com/samples/django/

When I run docker-compose up I get:

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

Dockerfile

# syntax=docker/dockerfile:1
FROM python:3
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1
WORKDIR /code
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

docker-compose.yml

version: "3.9"
   
services:
  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    environment:
      - POSTGRES_NAME=postgres
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=postgres
    depends_on:
      - db

settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': config('DB_NAME'),
        'USER': config('DB_USER'),
        'PASSWORD': config('DB_PASSWORD'),
        'HOST': config('DB_HOST'),
        'PORT': 5432,
    }
}
1
  • A few ways we can debug this issue, I am not saying any of this is a solution, just to debug it, can you replace command: python manage.py runserver 0.0.0.0:8000 with command: sleep 30 && python manage.py runserver 0.0.0.0:8000. Postgres takes a few seconds to start up, so this should confirm that is not an issue. And, use docker exec -it [container id] bash to enter the container and see if you can connect to the database running on db, you can use the psql command line tool for that. Commented Jan 3, 2022 at 4:55

1 Answer 1

0

Most probably your Postgres container is not starting up and crashing, since you haven't specified a default password. As per the docs - https://hub.docker.com/_/postgres,

The PostgreSQL image uses several environment variables which are easy to miss. The only variable required is POSTGRES_PASSWORD, the rest are optional.

To check this, run docker ps and see if the postgres container is running, if not, check the logs by docker container logs [Container id]

I'd recommend you should add the other variables as well, since without that, your Django application would not work. You need to add POSTGRES_DB and POSTGRES_USER as well.

Update the db service as below


  db:
    image: postgres
    volumes:
      - ./data/db:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: 
      POSTGRES_USER:
      POSTGRES_DB: 

Ensure those are the same as the ones in the Django config.

After this, you are still left with a couple of issues in your approach.

  1. Sending the right config in django. You are already setting the host in the configuration, you need to add the right username, password there as well.

  2. Initializing the database with tables, and populating data if required. Since you are using a volume, you can simply start the container, connect to the database locally(remember to add port mapping - https://docs.docker.com/compose/compose-file/compose-file-v3/#ports) and execute the SQL commands locally. Then kill the containers and restart them. However, there are better approaches. This is one question that addresses your issue - Docker initialize database tables and records in SQL Server. This is a popular problem and googling this specific problem can find a lot more answer depending on your usecase.

Sign up to request clarification or add additional context in comments.

1 Comment

Okay I think the problem was that I had my environment section in the web service. Now I get FATAL: password authentication failed for user "myuser". What I'm wondering now is how I can create the database user and run migrations so I can connect to a working DB.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.