Home > Mobile >  Azure App Service: How to run management command for dockerized django application
Azure App Service: How to run management command for dockerized django application

Time:09-21

I've a django applicaiton running in docker-compose in local along with an nginx and frontend applicaiton. I've tried to deploy the applicaiton in azure enter image description here

When i tried to connect SSH from azure portal using browser its showing connection closed.

enter image description here

Is there any other way to run django management commands in azure app service with a multi-container application.

Dockercompose

version: "3.8"

    services:
      web:
        image:  app.azurecr.io/app:latest
        container_name: app
        command: uwsgi --ini uwsgi.ini
        restart: always
        volumes:
          - volume:/code
        depends_on:
          - db
        environment:
           WEBSITES_ENABLE_APP_SERVICE_STORAGE: TRUE
        ports:
          - "2222:2222"    
      nginx:
        image: app.azurecr.io/nginx:latest 
        container_name: nginx
        restart: always
        volumes:
          - volume:/code
        environment:
          WEBSITES_ENABLE_APP_SERVICE_STORAGE: TRUE
        ports:
          - "80:80"
          - "2222:2222"
        depends_on:
          - web
      db:
       image:  app.azurecr.io/postgress-12:latest
       volumes: 
          - postgres_data:/var/lib/postgresql/data/
       ports:
          - 5432:5432
       restart: always
       environment:
           POSTGRES_USER: user
           POSTGRES_PASSWORD: pwd
           POSTGRES_DB: db
    volumes:
       postgres_data:
       volume:
          driver: local

Dockerfile

    FROM python:3.8

    # Set environment variables
    ENV PYTHONDONTWRITEBYTECODE 1
    ENV PYTHONUNBUFFERED 1

    # Set work directory
    WORKDIR /code

    # Install dependencies
    RUN apt-get update
    RUN apt-get -y install libgdal-dev
    RUN pip install  uwsgi
    RUN pip install --upgrade pip
    COPY ./requirements.txt /code/
    RUN pip install -r requirements.txt

    # Copy project
    COPY . /code/

    # Install OpenSSH and set the password for root to "Docker!". 
    RUN apt-get install -y openssh-server \
         && echo "root:Docker!" | chpasswd 

    # Copy the sshd_config file to the /etc/ssh/ directory
    COPY sshd_config /etc/ssh/

    # Copy and configure the ssh_setup file
    RUN mkdir -p /tmp
    COPY ssh_setup.sh /tmp
    RUN chmod  x /tmp/ssh_setup.sh \
        && (sleep 1;/tmp/ssh_setup.sh 2>&1 > /dev/null)
    RUN service ssh start    

    # Open port 2222 for SSH access
    EXPOSE 80 2222

SSHd

    Port            2222
    ListenAddress       0.0.0.0
    LoginGraceTime      180
    X11Forwarding       yes
    Ciphers aes128-cbc,3des-cbc,aes256-cbc,aes128-ctr,aes192-ctr,aes256-ctr
    MACs hmac-sha1,hmac-sha1-96
    HostkeyAlgorithms        ssh-rsa
    PubkeyAcceptedAlgorithms ssh-rsa
    StrictModes         yes
    SyslogFacility      DAEMON
    PasswordAuthentication  yes
    PermitEmptyPasswords    no
    PermitRootLogin     yes
    Subsystem sftp internal-sftp

CodePudding user response:

Run Django database migrations:

Django database migrations ensure that the schema in the PostgreSQL on Azure database match those described in your code.

  1. Open an SSH session in the browser by navigating to https://.scm.azurewebsites.net/webssh/host and sign in with your Azure account credentials (not the database server credentials).

  2. In the SSH session, run the following commands (you can paste commands using Ctrl Shift V):

    cd site/wwwroot

Activate default virtual environment in App Service container

source /antenv/bin/activate

Install packages

pip install -r requirements.txt

Run database migrations

python manage.py migrate

Create the super user (follow prompts)

python manage.py createsuperuser

The createsuperuser command prompts you for superuser credentials.

Inside dockerfile: add this:

RUN apt-get update \
    && chmod u x /app/init_container.sh

Add this also at the end of file:

ENTRYPOINT ["/projectname/EntrypointScriptfilename.sh"]

EX: ENTRYPOINT ["/app/init_container.sh"]

CodePudding user response:

You didn't define any commands your container should run on startup, so I think your container just quits and you can't connect to it because of that.

Create a file (init.sh for example) with your startup commands (run ssh, migrate the db, collectstatic & runserver) and point to that file at the bottom of your Dockerfile in an ENTRYPOINT command

CodePudding user response:

Your docker file should look like the same as below file:

FROM python:3.10-slim
WORKDIR /app/

COPY requirements.txt ./
RUN pip install -r requirements.txt
COPY ./ /app/

COPY sshd_config /etc/ssh/

# Start and enable SSH
RUN apt-get update \
    && apt-get install -y --no-install-recommends dialog \
    && apt-get install -y --no-install-recommends openssh-server \
    && echo "root:Docker!" | chpasswd \
    && chmod u x /app/init_container.sh

EXPOSE 8000 2222

ENTRYPOINT [ "/app/init_container.sh" ] #add your projectname instead of app

init_container.sh:

#!/bin/sh
set -e

#Get env vars in the Dockerfile to show up in the SSH session
eval $(printenv | sed -n "s/^\([^=]\ \)=\(.*\)$/export \1=\2/p" | sed 's/"/\\\"/g' | sed '/=/s//="/' | sed 's/$/"/' >> /etc/profile)

echo "Starting SSH ..."
service ssh start

# Start Gunicorn
exec gunicorn -b 0.0.0.0:8000 app:app
  • Related