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,
}
}
command: python manage.py runserver 0.0.0.0:8000withcommand: 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, usedocker exec -it [container id] bashto enter the container and see if you can connect to the database running ondb, you can use thepsqlcommand line tool for that.