0

I'm trying to run docker container with django + postgres.

When i run two containers (db + web) i see that it's ok on the db side:

LOG:  starting PostgreSQL 12.0 on x86_64-pc-linux-musl, compiled by gcc (Alpine 8.3.0) 8.3.0, 64-bit
LOG:  listening on IPv4 address "0.0.0.0", port 5432
LOG:  listening on IPv6 address "::", port 5432
LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
LOG:  database system was shut down at 2023-02-13 14:36:17 UTC
LOG:  database system is ready to accept connections

But on the django side i see the error:

django.db.utils.OperationalError: connection to server at "127.0.0.1", port 5432 failed: Connection refused
        Is the server running on that host and accepting TCP/IP connections?

DockerFile:

FROM python:3.10.0-alpine
WORKDIR /usr/src/app
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
RUN pip install --upgrade pip
RUN apk update
RUN apk add postgresql-dev gcc python3-dev musl-dev
COPY ./requirements.txt .
RUN pip install -r requirements.txt
COPY . .

docker-compose.yml:

version: '3.9'
services:
  web:
    build: .
    command: python manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/usr/src/app/
    ports:
      - 8000:8000
    env_file:
      - ./.env.dev
    depends_on:
      - db
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    environment:
      - POSTGRES_USER=my_admin
      - POSTGRES_PASSWORD=my_api
      - POSTGRES_DB=my_db

volumes:
  postgres_data:

Also, i have this setting in Django:

ALLOWED_HOSTS = ['127.0.0.1', 'localhost', '0.0.0.0', '::1']

And my .env file:

POSTGRES_DB=my_db
POSTGRES_USER=my_admin
POSTGRES_PASSWORD=my_api
POSTGRES_HOST=127.0.0.1
POSTGRES_PORT=5432

What i am doing wrong?

0 Answers0