Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Compose, Django: role "_" does not exist

Context
I am trying to run my Django application and Postgres database in a docker development environment using docker-compose (it's my first time using Docker). I want to use my application with a custom role and database both named teddycrepineau (as opposed to using the default postgres user and db).

Goal
My goal is to deploy a web app powered on the front end by react and the backend by django restapi, the whole running in a docker.

System/Version

  • python: 3.7
  • django: 2.1
  • OS: Mac OS High Sierra

What error am I getting
When running docker-compose up with my custom role and db, I am getting the following error django.db.utils.OperationalError: FATAL: role "teddycrepineau" does not exist. When running the same command with the default role and db postgres Django is able to start normally.

My understanding was that running docker-compose up would create the role and db passed as environment variable.

What I have tried so far
I read multiple threat on this site, GitHub, and docker:

  • tried to delete my container and rebuilt it with formatting as suggested here
  • Went through this GitHub issue
  • Tried to move my environment variable from .env file the environment inside my docker-compose.yml file and rebuild my container

Files

docker-compose.yml

version: '3'

volumes:
  postgres_data: {}

services:
  postgres:
    image: postgres
    volumes: 
      - postgres_data:/var/lib/postgresql/data
    env_file: .env
    ports:
      - "5432"

  django:
    build:
      context: teddycrepineau-backend
      dockerfile: teddycrepineau-root/Dockerfile
    command: ./teddycrepineau-backend/teddycrepineau-root/start.sh
    env_file: .env
    volumes: 
      - .:/teddycrepineau-backend
    ports:
      - "8000:8000"
    depends_on: 
      - postgres

Dockerfile

FROM python:3.7

ENV PYTHONUNBUFFERED 1

WORKDIR /teddycrepineau-backend/
ADD ./teddycrepineau-root/requirements.txt /teddycrepineau-backend/
RUN pip install -r requirements.txt
ADD . /teddycrepineau-backend/
RUN chmod +x ./teddycrepineau-root/start.sh

start.sh

#!/usr/bin/env bash
python3 ./teddycrepineau-backend/teddycrepineau-root/manage.py runserver

.env

POSTGRES_PASSWORD= 
POSTGRES_USER=teddycrepineau
POSTGRES_DB=teddycrepineau

EDIT

My file structure is as follow

root
 |___ teddycrepineau-backend
        |___ teddycrepineau-root
               |___ teddycrepineau
               |___ Dockerfile
               |___ manage.py
               |___ start.sh
 |___ teddycrepineau-frontend
        |___ React-App

 |___ .env
 |___ docker-compose.yml

When I move my docker-compose.yml file inside my backend folder, it starts as expected (though I am not able to access my site when going to 127.0.0.1:8000 but that is mostly a different issue) with custom user and db. When I put my docker-compose.yml file to my root folder, I get the error django.db.utils.OperationalError: FATAL: role "teddycrepineau" does not exist

like image 718
Teddy Avatar asked Jan 14 '19 01:01

Teddy


2 Answers

This happens because your pgsql db was launched without any envs. The pgsql docker image only uses the envs the first time you created the container, after that it won't recreate DB and users.

The solution is to remove the pgsql volume so next time you docker-compose up you will have a fresh db with envs read. Simple way to do it is docker-compose down -v

like image 134
Siyu Avatar answered Sep 22 '22 17:09

Siyu


Change your env order like this.

POSTGRES_DB=teddycrepineau
POSTGRES_USER=teddycrepineau
POSTGRES_PASSWORD= 

I find it at this issue. I hope it works.

like image 30
Jrog Avatar answered Sep 23 '22 17:09

Jrog