Create django super user in a docker container without inputting password
Get the container ID and run the command.
docker exec -it container_id python manage.py createsuperuser
I recommend adding a new management command that will automatically create a superuser if no Users exist.
See small example I created at https://github.com/dkarchmer/aws-eb-docker-django. In particular, see how I have a python manage.py initadmin
which runs:
class Command(BaseCommand):
def handle(self, *args, **options):
if Account.objects.count() == 0:
for user in settings.ADMINS:
username = user[0].replace(' ', '')
email = user[1]
password = 'admin'
print('Creating account for %s (%s)' % (username, email))
admin = Account.objects.create_superuser(email=email, username=username, password=password)
admin.is_active = True
admin.is_admin = True
admin.save()
else:
print('Admin accounts can only be initialized if no Accounts exist')
(See Authentication/management/commands).
You can see how the Dockerfile then just runs CMD to runserver.sh which basically runs
python manage.py migrate --noinput
python manage.py initadmin
python manage.py runserver 0.0.0.0:8080
Obviously, this assumes the Admins immediately go change their passwords after the server is up. That may or may not be good enough for you.