skip to Main Content

I have a problem with my django + postgresql docker compose.

docker-compose.yml:

version: '3.8'

services:
  db:
    image: postgres:12.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.env
  web:
    build: .
    command: python MoreEnergy/manage.py runserver 0.0.0.0:8000
    volumes:
      - ./MoreEnergy/:/MoreEnergy/
    ports:
      - 8000:8000
    env_file:
      - ./.env
    depends_on:
      - db

volumes:
  postgres_data:

Dockerfile:

FROM python:3.9-alpine

WORKDIR /app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install psycopg2 dependencies
RUN apk update 
    && apk add postgresql-dev gcc python3-dev musl-dev

# install dependencies
COPY requirements.txt /app/requirements.txt
RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt


# copy project
COPY . .

db container check:

(shell) $ docker compose exec db psql --username=dmitriy --dbname=more_energy_db

psql (12.0)
Type "help" for help.

more_energy_db=# l
                                  List of databases
      Name      |  Owner  | Encoding |  Collate   |   Ctype    |  Access privileges  
----------------+---------+----------+------------+------------+---------------------
 more_energy_db | dmitriy | UTF8     | en_US.utf8 | en_US.utf8 | 
 postgres       | dmitriy | UTF8     | en_US.utf8 | en_US.utf8 | 
 template0      | dmitriy | UTF8     | en_US.utf8 | en_US.utf8 | =c/dmitriy         +
                |         |          |            |            | dmitriy=CTc/dmitriy
 template1      | dmitriy | UTF8     | en_US.utf8 | en_US.utf8 | =c/dmitriy         +
                |         |          |            |            | dmitriy=CTc/dmitriy
(4 rows)

more_energy_db=# 

error:

django.db.utils.OperationalError: could not connect to server: Connection refused
        Is the server running on host "127.0.0.1" and accepting
        TCP/IP connections on port 5432?

As you can see, db container works correct, but web container don’t see database. My .env file has all needed vars to up containers What should I do?

2

Answers


  1. I can’t see your env for your Django app, but based on your error, I think its because you are trying to connect to the postgres(port 5432) inside your django container (127.0.0.1), which is not present there of course.

    The thing you should do is set your Django app env to connect to your db container like this for example db:5432 (inside the env) (usually accessible using service name within same compose file)

    More details about this can be found here.

    Login or Signup to reply.
  2. You have to set host = localhost in your settings.py, as according to create service inside you docker file.

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'dbname',
            'USER': 'user',
            'PASSWORD': 'password',
            'HOST': 'localhost'
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search