1

I am dockerizing an existing Django application, but am struggling with initializing a database and connecting. My db service starts up with the following config, which looks right to me:

db:
build:
  context: /Users/ben/Projects/project-falcon/project-falcon-containers
  dockerfile: Dockerfile-db-local
environment:
  POSTGRES_DB: falcon_db
  POSTGRES_PASSWORD: falcon
  POSTGRES_USER: falcon
ports:
- 5432:5432/tcp
restart: always

At this point, I would expect the separate api container to be able to connect but instead I get django.db.utils.OperationalError: FATAL: role "falcon" does not exist

the relevant settings file:

from .dev import *

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': os.environ['DB_NAME'],
        'USER': os.environ['DB_USER'],
        'PASSWORD': os.environ['DB_PASS'],
        'HOST': os.environ['DB_SERVICE'],
        'PORT': os.environ['DB_PORT']
    }
}

which is built in api and configured thus

api:
 build:
   context: /Users/ben/Projects/falcon/falcon-backend
   dockerfile: Dockerfile
 depends_on:
 - db
 - redis
 environment:
   DB_NAME: falcon_db
   DB_PASS: falcon
   DB_PORT: '5432'
   DB_SERVICE: db
   DB_USER: falcon
 ports:
 - 8001:8001/tcp
 volumes:
 - /Users/ben/Projects/falcon/falcon-backend:/falcon:rw

So based on the documentation for the postgres container I expected the POSTGRES_* variables to create the user/database Django expects to connect as/to. But the error message sure makes me think it did not. So based on the docs and the following question:

How to create User/Database in script for Docker Postgres

I tried the older method of specifying a specific .sql file to run

#init.sql

CREATE USER falcon WITH PASSWORD 'falcon';
CREATE DATABASE falcon_db;
grant all privileges on database falcon_db to falcon;
ALTER USER falcon CREATEDB;
CREATE EXTENSION adminpack;

which is specified in the db service's Dockerfile

FROM postgres:alpine
COPY init.sql /docker-entrypoint-initdb.d/

Do I understand the documents correctly? Am I interpreting the error correctly? Is there a problem with the unix users on the VM? Any help will be much appreciated.

Ben
  • 4,980
  • 3
  • 43
  • 84
  • Yes, you understand the documentation correctly. It must work. What problem do you have in the end? – Bukharov Sergey Feb 26 '18 at 19:59
  • the api connection attempt when Django bootstraps yields: `django.db.utils.OperationalError: FATAL: role "falcon" does not exist` – Ben Feb 26 '18 at 20:00
  • Be sure you do not have any mounted volumes. And also try to destroy and recreate containers with `docker-compose down` and `docker-compose up` commands. – Bukharov Sergey Feb 26 '18 at 20:04
  • i've been using `docker-compose` for all of this, and there are no (explicitly) mounted volumes in either `docker-compose.yaml` or the `Dockerfile`s, however, running `docker-compose down` stopped WAY more `db_*` services than I expected, so looks like I may have not cleaned up properly... – Ben Feb 26 '18 at 20:15
  • so, does it work now? – Bukharov Sergey Feb 26 '18 at 20:22

0 Answers0