Connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused

I have an issue with my container. I tried to Dockerize my django project and now i get some errors that i cannot resolve this is my Dockerfile

FROM python:3.9-slim-buster

ENV PYTHONUNBUFFERED 1

RUN mkdir /code
WORKDIR /code
COPY requirements.txt /code/
RUN apt-get update && apt-get install -y libpq-dev
RUN apt-get update && apt-get install -y python3-psycopg2
RUN pip3 install Psycopg2-binary
RUN pip install --upgrade pip && pip install -r requirements.txt
COPY . /code/`

this is my 'docker-compose.yml'

version: '3'
services:
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - localhost
  localhost:
    image: postgres:10
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=*****
      - POSTGRES_DB=test_DB_IS
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    ports:
      - "5432:5432"
volumes:
    postgres_data:

But my command 'docker-compose up --build' with this output :

mbakop_polls-web-1        |     connection = Database.connect(**conn_params)
mbakop_polls-web-1        |   File "/usr/local/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect
mbakop_polls-web-1        |     conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
mbakop_polls-web-1        | django.db.utils.OperationalError: connection to server at "localhost" (127.0.0.1), port 5432 failed: Connection refused
mbakop_polls-web-1        |     Is the server running on that host and accepting TCP/IP connections?
mbakop_polls-web-1        | connection to server at "localhost" (::1), port 5432 failed: Cannot assign requested address
mbakop_polls-web-1        |     Is the server running on that host and accepting TCP/IP connections?
mbakop_polls-web-1 

This is my 'docker-compose.yml'

version: '3'
services:
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - localhost
  localhost:
    image: postgres:10
    environment:
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=******
      - POSTGRES_DB=test_DB_IS
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    ports:
      - "5432:5432"
volumes:
    postgres_data:

How can i fix it ?

Back to Top