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 app service using my custom docker image for the django application.
The app deployment was successfull but now i need to run django management command like python manage.py migrate
and python manage.py createsuperuser
.
I tried to use SSH in my django container, but when i tried to connect its showing
az webapp create-remote-connection --subscription <id> --resource-group <rg-name> -n <app-anme> &
When i tried to connect SSH from azure portal using browser its showing connection closed.
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
3
Answers
Your docker file should look like the same as below file:
init_container.sh:
Run Django database migrations:
Django database migrations ensure that the schema in the PostgreSQL on Azure database match those described in your code.
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).
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
Run database migrations
Create the super user (follow prompts)
The createsuperuser command prompts you for superuser credentials.
Inside dockerfile:
add this:
Add this also at the end of file:
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