4

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
4
  • The official docs seem to point to a different Dockerfile. Have you tried using that instead? Commented Jul 5, 2017 at 17:58
  • I am not sure you meant what I have added to post? Commented Jul 5, 2017 at 18:02
  • I think maybe you are confused how the Dockerfile works with regards to multiple FROM instructions. Can you also post your docker-compose.yml? What you are currently doing isn't possible but I can offer a suggestion. Commented Jul 5, 2017 at 18:58
  • I will be grateful. I have updated post. Commented Jul 5, 2017 at 19:00

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much, perfect answer, everything works properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.