Docker cannot assign requested address in Django

When I request the docker-compose up --build command, docker gives me an error: prediction-1 | django.db.utils.OperationalError: connection is bad: Cannot assign requested address prediction-1 | Is the server running on host "::1" and accepting prediction-1 | TCP/IP connections on port 5432?

I downloaded PostgreSql from the official website.

My settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ.get("DB_NAME"),
        'USER': os.environ.get("DB_USER"),
        'PASSWORD': os.environ.get("DB_PASSWORD"),
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

My docker-compose.yml

version: '3.8'

services:
  postgres:
    restart: always
    image: postgres:latest
    container_name: postgres-db
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: 5332Dialog
      POSTGRES_DB: site
    ports:
      - "5432:5432"
  prediction:
    build:
      context: ./mysite
    ports:
      - "8000:8000"
    depends_on:
      - postgres
    environment:
      DEBUG: "False"
      DB_USER: "postgres"
      DB_PASSWORD: 
      DB_HOST: "postgres"

My Dockerfile

FROM python:3.10.4

WORKDIR /app
COPY . /app/

RUN apt-get update && apt-get install -y python3 && pip install -r requirements.txt

CMD ["bash", "-c", "python manage.py migrate && python manage.py loaddata categories.json && python manage.py loaddata things.json && python manage.py runserver 0.0.0.0:8000"]

I wanted to learn how to use Docker using your text this video: https://www.youtube.com/watch?v=0uLDObuutFs&ab_channel=ElenaDeykun-PythonBlog but somehow it didn’t work out that way

Back to Top