0

I have a problem with connecting Django with PostgreSQL installed in docker, when run Django using

python manage.py runserver

it returns the following error

OperationalError: could not translate host name "db" to address: Unknown host

settings.py:

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

Dockerfile:

# pull official base image
FROM python:3.6.4-alpine

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install wkhtmltopdf dependencies
RUN wget https://s3.amazonaws.com/shopify-managemant-app/wkhtmltopdf-0.9.9-static-amd64.tar.bz2
RUN tar xvjf wkhtmltopdf-0.9.9-static-amd64.tar.bz2
RUN mv wkhtmltopdf-amd64 /usr/local/bin/wkhtmltopdf
RUN chmod +x /usr/local/bin/wkhtmltopdf

# install python dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN apk --update add libxml2-dev libxslt-dev libffi-dev gcc musl-dev libgcc openssl-dev curl
RUN apk add jpeg-dev zlib-dev freetype-dev lcms2-dev openjpeg-dev tiff-dev tk-dev tcl-dev
RUN \
 apk add --no-cache postgresql-libs && \
 apk add --no-cache --virtual .build-deps gcc musl-dev postgresql-dev && \
 python3 -m pip install -r requirements.txt --no-cache-dir && \
 apk --purge del .build-deps

# copy project
COPY . /usr/src/app/

docker-compose:

version: '3.7'

services:
  db:
    image: postgres:13.0
    restart: always
    environment:
      POSTGRES_PASSWORD: trytofindme
    ports:
      - 15432:5432
  adminer:
    image: adminer
    restart: always
    ports:
      - 8020:8080

I can't find mistake in my code. Are there any variant to connect PostgreSQL and Django, not loosing the protection?

I used:

docker-compose up -d --build

And then:

docker-compose up

Docker container starts working and said, that db was ready to accept the connections. So I think, that problem is in my Django setup

1 Answer 1

1

The host has to be a hostname, so an address to your database. In your case, it should be set to either localhost or 127.0.0.1. Django has no knowledge of the logical names of your Docker services.

Your docker-compose.yaml also specifies a different host port than the ones you put in your Django config (see docs):

Either specify both ports (HOST:CONTAINER), or just the container port (an ephemeral host port is chosen).

You should either change the port in your Django config to 15432, or better change the port mapping as follows

...

services:
  db:
    ...
    ports:
      - "5432:5432"
...

Using a standard port like 5432 is preferable because it makes it easier to identify host services/processes from the open ports on the host.

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

3 Comments

The problem now is: " Is the server running on host "localhost" (127.0.0.1) and accepting TCP/IP connections on port 5432?"
So, the problem was in port, written in settings.py. Db is running on port 15432, but i mentioned 5432 in it. Thank you for your help, sir.
Yes, as I just added in the answer :) No worried, glad your problem is solved!

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.