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.