I'm trying to learn how docker works, and trying to set up a react/Django image with Postgres, but I can't seem to connect to the database:
This is my docker-compose.yml
version: "3"
services:
database:
image: postgres:12.7-alpine
volumes:
- ./backup_data/db:/var/lib/postgresql/data
environment:
- POSTGRES_DB=postgres
- POSTGRES_USER=postgres
- POSTGRES_PASSWORD=postgres
backend:
build: ./mash_backend
volumes:
- ./backend:/app
depends_on:
- database
frontend:
build: ./mash_frontend
volumes:
- ./frontend:/app
depends_on:
- backend
ports:
- 80:80
nginx_backend_server:
build: ./nginx_backend_server
ports:
- 8000:8000
depends_on:
- backend
and on my seetings.py, I placed the host as database since this is the name of my container, not sure if this is correct
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'postgres',
'USER': 'postgres',
'PASSWORD': 'postgres',
'HOST': 'database',
'PORT': 5432,
}
}
this is my Dockerfile on my backend:
FROM python:3.8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
COPY . /app
WORKDIR /app
RUN pip3 install -r req.txt
RUN python manage.py makemigrations
RUN python manage.py migrate
CMD gunicorn -b 0.0.0.0:8000 --worker-class=gevent --worker-connections=1000 --workers=5 backend.wsgi
whenever I run a docker-compose up I got this error during the migrate
psycopg2.OperationalError: could not translate host name "database" to address: Name or service not known
what I'm missing here?