skip to Main Content

I have 3 environments set up and cannot createsuperuser. The way I migrate and runserver now follows the container so I have an entrypoint.sh:

#!/bin/sh
  1
  2 echo "${RTE} Runtime Environment - Running entrypoint."
  3
  4 if [ "$RTE" = "dev" ]; then
  5
  6     python manage.py makemigrations --merge
  7     python manage.py migrate --noinput
  8     python manage.py runserver 0:8000
  9
 10     python manage.py createsuperuser --username "admin" --email "[email protected]" --password "superuser"
 11     echo "created superuser"
 12
 13 elif [ "$RTE" = "test" ]; then
 14
 15     echo "This is tets."
 16     python manage.py makemigrations
 17     python manage.py migrate
 18     python manage.py runserver
 19
 20 elif [ "$RTE" = "prod" ]; then
 21
 22     python manage.py check --deploy
 23     python manage.py collectstatic --noinput
 24     gunicorn kea_bank.asgi:application -b 0.0.0.0:8080 -k uvicorn.workers.UvicornWorker
 25
 26 fi

lines 10/11 is what I want to make work, I think I have a syntax issue. Originally I wanted the password and username to be variables stored in an env file:

1   RTE=dev
  1 POSTGRES_USER=pguser
  2 POSTGRES_PASSWORD=pgpassword
  3 POSTGRES_DB=devdb
  4 DJANGO_SUPERUSER_PASSWORD=superuser
  5 [email protected]
  6 DJANGO_SUPERUSER_USERNAME=admin

But now I just want it to work, I need to create a superuser account in order to develop.
Can anyone spot what’s wrong?
I can see my application on localhost:8000, but how do I create the superuser in this scenario?

2

Answers


  1. According to createsuperuser -h and this doc, createsuperuser command does not support --password flag. to read arguments from environment variables, you should use this command with --noinput flag and set required fields like username, email and password as DJANGO_SUPERUSER_<uppercase_field_name> in your env file.

    python manage.py createsuperuser --noinput

    Login or Signup to reply.
  2. Automate / Create django superuser during docker – compose up.

    Create a python file.Like

    createsuperuser.py

    Keep this file with django project. sometimes docker-compose can not find this because of wrong place of this file. so be careful.

    from django.db import IntegrityError
    from django.contrib.auth.models import User
    
    import environ # This package import from 'django-environ' https://pypi.org/project/django-environ/
    env = environ.Env(
        DEBUG=(bool, False)
    )
    
    try:
        superuser = User.objects.create_superuser(
            username=env('SUPER_USER_NAME', default="abcd"),
            email=env('SUPER_USER_EMAIL', default="[email protected]"),
            password=env('SUPER_USER_PASSWORD', default="abc"))
        superuser.save()
    except IntegrityError:
        print(f"Super User with username {env('SUPER_USER_NAME')} is already exit!")
    except Exception as e:
        print(e)
    

    Now in docker-compose.yml under the django project image in command section add. I’m adding details docker-compose file so that you can get help for others section

    python manage.py shell < createsuperuser.py

    backend:
        build:
          context: .
          dockerfile: docker/backend/Dockerfile
        image: backend:pos-backend
        container_name: pos_backend
        volumes:
          - ./backend:/home/pos
          - static:/home/pos/staticfiles
          - media:/home/pos/mediafiles
        environment:
          DJANGO_SETTINGS_MODULE: pos.settings
        command: >
          /bin/bash -c "python manage.py makemigrations  --noinput
          && python manage.py migrate  --noinput
          && python manage.py shell < createsuperuser.py
          && python manage.py collectstatic  --noinput
          && gunicorn pos.wsgi:application --bind 0.0.0.0:8000"
        ports:
          - "8000:8000"
        expose:
          - 8000
        env_file:
          - ./backend/.env.dev
        depends_on:
          - db
        restart: on-failure
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search