user7304253
user7304253

Reputation:

Creating postgresql database in Docker with Django project

I would like to create postgresql database in docker with my Django project. I'm trying to do it using init.sql file but it doesn't work:

settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'aso',
        'USER': 'postgres',
        'HOST': 'db',
        'PORT': 5432,
    }
}

init.sql:

CREATE USER postgres;
CREATE DATABASE aso;
GRANT ALL PRIVILEGES ON DATABASE aso TO postgres;

My updated Dockerfile:

FROM python:3.6.1
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip3 install -r requirements.txt
ADD . /code/
FROM library/postgres
ADD init.sql /docker-entrypoint-initdb.d/

Unfortunately I get:

db_1   | LOG:  database system was shut down at 2017-07-05 14:02:41 UTC
web_1  | /usr/local/bin/docker-entrypoint.sh: line 145: exec: python3: not found
db_1   | LOG:  MultiXact member wraparound protections are now enabled
db_1   | LOG:  autovacuum launcher started
db_1   | LOG:  database system is ready to accept connections
dockerpri_web_1 exited with code 127

I was trying with this Dockerfile, too:

FROM python:3.6.1
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip3 install -r requirements.txt
ADD . /code/
FROM library/postgres
ENV POSTGRES_USER docker
ENV POSTGRES_PASSWORD docker
ENV POSTGRES_DB aso

My docker-compose.yml:

version: '3'

services:
  db:
    image: postgres
  web:
    build: .
    command: python3 PROJECT/backend/project/manage.py runserver 0.0.0.0:8001
    volumes:
      - .:/code
    ports:
      - "8001:8001"
    depends_on:
      - db

Upvotes: 4

Views: 3436

Answers (1)

Andy Shinn
Andy Shinn

Reputation: 28483

First, your Dockerfile example isn't valid. It should just have one FROM instruction. Remove the PostgreSQL stuff so it is just:

FROM python:3.6.1
ENV PYTHONUNBUFFERED 1
RUN mkdir /code
WORKDIR /code
ADD requirements.txt /code/
RUN pip3 install -r requirements.txt
ADD . /code/

Next, I am going to answer the question of how to create a PostgreSQL user and database since that is what your init.sql is doing.

According to the postgres image documentation at https://hub.docker.com/_/postgres/, there are POSTGRES_USER and POSTGRES_PASSWORD environment variables available to us. The POSTGRES_USER variable will create a user and database with privileges to that user. An example amend to your docker-compose.yml would be:

version: '3'

services:
  db:
    image: postgres
    environment:
      POSTGRES_USER: aso
      POSTGRES_PASSWORD: somepass
  web:
    build: .
    command: python3 PROJECT/backend/project/manage.py runserver 0.0.0.0:8001
    volumes:
      - .:/code
    ports:
      - "8001:8001"
    depends_on:
      - db

On start, this will initialize the PostgreSQL database with a username of aso, a database of aso, and a password of somepass. Then, you can just amend your settings.py to use this username, database, and password.

I'll also add, that if you wanted to run other arbitrary SQL or scripts during database startup, you can do so by adding sql or sh files to the postgres image at /docker-entrypoint-initdb.d/. Read more about this at https://github.com/docker-library/docs/tree/master/postgres#how-to-extend-this-image.

Upvotes: 3

Related Questions