0

I'm attempting to write a script for the initial setup of superuser in django. I have a docker container running django.

docker exec -it "mydockername" bash -c echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('root3', '[email protected]', 'root3')" | python manage.py shell

This script is throwing an error

      File "manage.py", line 16
    ) from exc
         ^
SyntaxError: invalid syntax

But if I execute the command

echo "from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('root3', '[email protected]', 'root3')" | python manage.py shell

inside of the docker container I don't have any problems and the superuser is created.

2 Answers 2

1

You need to quote the last half of your command so that bash -c gets a single argument. This makes the entire command kind of complicated (since it already uses both single and double quotes), but it'd look like

docker exec -it "mydockername" \
  bash -c "echo \"from django.contrib.auth import ...\" | python manage.py shell"

Without this, your local shell gets first stab at picking the command apart and it sees

docker exec ... | python manage.py shell

And furthermore, since bash -c accepts only a single argument, the command getting run inside the container is

bash -c echo  # with another argument that gets ignored

You could probably run this command directly on your host (without docker exec). You'd need to write an alternate settings.py file that points at the Docker-hosted database (the database host name would be localhost and its port whatever is published via ports:) and then those settings could reach the database inside the container.

Probably the best path here is to avoid writing a Python script run via a quoted shell command launched through docker exec. Write a small extension script, as discussed in the Django documentation Writing custom django-admin commands page:

from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
class CreateSuperuser(BaseCommand):
  def handle(self, *args, **options):
    User = get_user_model()
    User.objects.create_superuser('root3', '[email protected]', 'root3')

Place this in management/commands/superuser.py, and you can run python manage.py superuser (potentially via docker exec). The Django documentation discusses ways to add arguments and write output.

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

1 Comment

Thank you to much, now I understand my mistake and you are right I should create a custom command in django .
1

Try this:

docker exec -it "mydockername" bash -c "echo \"from django.contrib.auth import get_user_model; User = get_user_model(); User.objects.create_superuser('root3', '[email protected]', 'root3')\" | python manage.py shell"

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.