0

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?

Raul Quinzani
  • 493
  • 1
  • 4
  • 16
  • `'HOST': 'database'` You have a _container_ named "database", but I don't think that is the same as having a _host_ named "database". – John Gordon Mar 26 '23 at 00:31
  • It actually works like that, since docker-compose creates a local network with service name. I also connect my django to DB like that, so I can vouch. – COSHW Mar 26 '23 at 00:40
  • 2
    This question seems related https://stackoverflow.com/q/51750715/494134 – John Gordon Mar 26 '23 at 00:49
  • You can never connect to a database or another container in the Dockerfile; the image it produces contains your application code but no other side effects that might happen during the image build process. [How do you perform Django database migrations when using Docker-Compose?](https://stackoverflow.com/questions/33992867/how-do-you-perform-django-database-migrations-when-using-docker-compose) seems like it might address the larger problem. – David Maze Mar 26 '23 at 01:30

1 Answers1

1

Never had this issue, but I can recommend to take out the migration code to a temporary container and making it dependent on the backend container, this will solve this problem by removing RUN commands from your Dockerfile (thx to the comments for the reason specifications).

Example of a migration compose service:

  do-migration:
    build: ./mash_backend
    depends_on:
      - backend
    command:
      - /bin/bash
      - -c
      - |
        echo "Apply migration"
        python manage.py makemigrations
        python manage.py migrate
        exit 0
    volumes:
      - ./backend:/app

This of course means that you'll have to take out migration commands from your Dockerfile. Hope this helps.

COSHW
  • 146
  • 8