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?

4
  • 'HOST': 'database' You have a container named "database", but I don't think that is the same as having a host named "database". Commented Mar 26, 2023 at 0: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. Commented Mar 26, 2023 at 0:40
  • 2
    This question seems related stackoverflow.com/q/51750715/494134 Commented Mar 26, 2023 at 0: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? seems like it might address the larger problem. Commented Mar 26, 2023 at 1:30

1 Answer 1

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.

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

Comments

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.