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