在Docker容器中创建django超级用户而无需输入密码 [英] Create django super user in a docker container without inputting password

查看:342
本文介绍了在Docker容器中创建django超级用户而无需输入密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在带有结构的Django docker容器中创建createsuperuser。



要在Django中创建超级用户,我需要在Django交互模式下运行此命令:

  ./ manage.py createsuperuser 

并且因为我想使其在结构脚本中运行,所以我找到了> 命令可以避免输入密码

  echo从django.contrib.auth.models导入User; User.objects.create_superuser('admin','admin@example.com','pass') | ./manage.py shell 

然后我将其与 docker exec一起使用在我的Django容器中运行

  docker exec container_django echo from django.contrib.auth.models import User; User .objects.create_superuser('admin','admin@example.com','pass') | ./manage.py shell 

问题出在linux管道,管道(|)全部

这不仅是困难的部分,考虑将所有这些垃圾进入结构运行,这意味着它们两端都需要引号。这将使整个事情变得非常紧急。

 结构运行:
运行( docker exec container_django {创建django超级用户的命令})

我仍在努力如何至少在结构运行中进行垃圾工作,但我不知道

解决方案

我建议添加一个新的管理命令,如果没有用户,该命令将自动创建超级用户。

p>

查看我在 https创建的小示例: //github.com/dkarchmer/aws-eb-docker-django 。特别是,看看我如何运行一个 python manage.py initadmin

  class Command(BaseCommand):

def handle(self,* args,** options):
if Account.objects.count()== 0:
适用于设置中的用户。管理员:
用户名= user [0] .replace('')
电子邮件= user [1]
密码='admin'
打印(为%s(%s)创建帐户%(用户名,电子邮件))
admin = Account.objects.create_superuser(电子邮件=电子邮件,用户名=用户名,密码=密码)
admin.is_active =真
admin.is_admin =真
admin.save()
否则:
print('管理员帐户只能在没有帐户的情况下初始化')

(请参阅身份验证/管理/命令)。



您可以看看Dockerfile然后如何只运行CMD到runserver.sh基本运行

  python manage.py migration --noinput 
python manage.py initadmin
python manage.py py runserver 0.0.0.0:8080

很明显,这是假定管理员在服务器后立即去更改密码起来了。这可能对您来说不够好。


I am tring to createsuperuser in a django docker container with fabric.

To create the super user in django, I need run this in a django interactive mode:

./manage.py createsuperuser

And because I want to make it run in a fabric script, so I find this command could avoid inputing password

echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@example.com', 'pass')" | ./manage.py shell

Then I put this together with "docker exec" to run it in my django container

docker exec container_django echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'admin@example.com', 'pass')" | ./manage.py shell

The problem comes out with the linux pipe, the pipe(|) all the contents on its left(including the docker exec) to its right(./manage.py shell)

And this is not only difficult part, considering to put all these junks into a fabric run, which means they need quotes on both end. It will make whole thing very urgly.

fabric run:
run("docker exec container_django {command to create django super user}")

I am still struggling on how to make at least the junk work in a fabric run, but I don't know how to do it.

解决方案

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.

这篇关于在Docker容器中创建django超级用户而无需输入密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆