Skip to main content

Dockerizing a Django app with scripted super user creation

I recently dockerized a small Django application. I build the Dockerfile in a way that the resulting image would allow running the container as if it was plain, e.g. that besides docker-compose up I could also do:

# For a psql session into the database:
docker-compose run <image_name> dbshell

# Or, to run the test suite:
docker-compose run <image_name> test

To make that work, I made this Docker entrypoint script:

#! /bin/bash
# Copyright (C) 2018 Sebastian Pipping <>
# Licensed under CC0 1.0 Public Domain Dedication.

set -e
set -u

RUN() {
    ( PS4='# ' && set -x && "$@" )

RUN wait-for-it "${POSTGRES_HOST}:${POSTGRES_PORT}" -t 30

cd /app

if [[ $# -gt 0 ]]; then
    RUN ./ "$@"
    RUN ./ makemigrations
    RUN ./ migrate
    RUN ./ createcustomsuperuser  # self-made

    RUN ./ runserver${APP_PORT}

Management command createcustomsuperuser is something simple that I built myself for this very purpose: Create a super user, support scripting, accept a passwords as bad as "password" or "demo" without complaints, and be okay if the user exists with the same credentials already (idempotency). I uploaded as a Gist to GitHub as it's a few lines more. Back to the entrypoint script. For the RUN ./ "$@" part to work, in the Dockerfile both ENTRYPOINT and CMD need to use the [..] syntax, e.g.:

ENTRYPOINT ["/app/"]
CMD []

For more details on ENTRYPOINT quirks like that I recommend John Zaccone's well-written article "ENTRYPOINT vs CMD: Back to Basics".