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.