简体   繁体   中英

django superuser created through docker-compose file doesn't work

I have a simple django-mongo application. I have a dockerfile for my django application.

I have a docker-compose.yml which contains django and mongo images. I am able to build and run the django-mongo application using docker-compose commands.

the problem I am facing is I am unable to log into the django admin-panel using superuser credentials.

Below is my dockerfile

FROM python:3.8-alpine

RUN mkdir /cursor_infotech
WORKDIR /cursor_infotech
ADD . /cursor_infotech/

RUN pip install -r requirements.txt

docker-compose.yml

version: "3"
services:
       cursor-infotech:
           container_name: cursor
           image: cursor
           restart: always
           build: .
           #environment:
            #  - MONGO_URI=$MONGO_URI
            #  - PORT=$PORT
            #  - NODE_ENV=$NODE_ENV   
           ports:
              - "7000:7000"
           networks:
              - cursor-backend
           depends_on:
              - mongo
           command: > 
                sh -c "python manage.py makemigrations && python manage.py migrate && 
                python manage.py createsuperuser --noinput --username admin --email admin@test.com && gunicorn -b 0.0.0.0:7000 cursor_infotech.wsgi"
       mongo:
             container_name: mongo
             image: mongo  
             environment:
               - MONGO_INITDB_DATABASE=cursor-pcbuild
               - MONGO_INITDB_ROOT_USERNAME=root
               - MONGO_INITDB_ROOT_PASSWORD=password
             volumes:
                - ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo-js:ro
                - ./data:/usr/share/db/data
             ports:
                - '27017:27017'
             networks:
                - cursor-backend 
networks:
  cursor-backend:
    driver: bridge

I ran this command in my project folder

docker-compose up --build

The django & mongo images are build and deployed - working fine.

When I try to log-into admin account in my django admin-panel, I get below error.

django admin panel error

the django superuser I created using docker-compose.yml doesn't work when I try to log into the django admin-panel. How to solve this issue?

Update:

I have already created an.env file in the same directory as docker-compose.yml. ( forgot to mention it )

.env file as show below

DJANGO_SUPERUSER_PASSWORD=password

As mentioned in below comment, I am explaining the issue if you don't wish to look at the image. -> when i try to access the admin-panel using my username and password,

I get error: please enter correct username and password for a staff account.

As per the documentation :

"When run non-interactively, you can provide a password by setting the DJANGO_SUPERUSER_PASSWORD environment variable. Otherwise, no password will be set, and the superuser account will not be able to log in until a password has been manually set for it."

As such you will need to set the environment variable before the createsuperuser command is ran.

Additionally please refrain from posting errors as images, it can cause problems for users with slow connection or accessibility software running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM