You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure App Service中如何为Docker化Django应用执行管理命令

在Azure App Service多容器环境中执行Django管理命令的替代方案

我本地用docker-compose运行Django+Nginx+前端的应用,已按官方流程部署到Azure App Service,应用运行正常,但现在需要执行python manage.py migrate、python manage.py createsuperuser这类Django管理命令。尝试通过SSH连接Django容器时遇到问题:用Azure CLI命令az webapp create-remote-connection --subscription <id> --resource-group <rg-name> -n <app-anme> &失败;通过Azure门户浏览器端SSH连接时,连接直接关闭。想知道在Azure App Service多容器应用里,还有什么方法能执行这些管理命令?


提供的配置文件

Docker Compose

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_config

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

解决方案

方法1:直接用Azure CLI远程执行命令

Azure App Service支持直接针对指定容器执行命令,无需SSH连接:

  • 执行非交互式命令(如迁移):
    az webapp exec --subscription <订阅ID> --resource-group <资源组名> --name <应用名> --container-name app --command "python manage.py migrate"
    
  • 执行交互式命令(如创建超级用户):
    添加--stdin参数进入交互模式,按提示输入信息即可:
    az webapp exec --subscription <订阅ID> --resource-group <资源组名> --name <应用名> --container-name app --stdin --command "python manage.py createsuperuser"
    

方法2:容器启动时自动执行命令

修改Django容器的启动命令,让它在启动uWSGI前自动执行管理命令:

  • 仅自动执行迁移:
    修改docker-compose.yml中web服务的command:
    command: >
      sh -c "python manage.py migrate && uwsgi --ini uwsgi.ini"
    
  • 自动创建超级用户(需配合环境变量):
    先在web服务的environment中添加超级用户相关变量,再修改启动命令:
    environment:
      WEBSITES_ENABLE_APP_SERVICE_STORAGE: TRUE
      DJANGO_SUPERUSER_USERNAME: admin
      DJANGO_SUPERUSER_PASSWORD: your_secure_password
      DJANGO_SUPERUSER_EMAIL: admin@example.com
    command: >
      sh -c "python manage.py migrate && python manage.py createsuperuser --noinput && uwsgi --ini uwsgi.ini"
    
    注:--noinput参数会跳过交互式输入,直接使用环境变量的值创建用户。

方法3:修复SSH连接问题(可选)

如果坚持要用SSH连接,需要解决几个关键问题:

  1. 端口冲突:你的docker-compose.yml中web和nginx都映射了2222端口,Azure App Service只能暴露一个容器的2222端口,需删除nginx服务的- "2222:2222"端口映射。
  2. sshd未随容器启动:Dockerfile中的RUN service ssh start仅在镜像构建时执行,容器启动时不会自动启动sshd。需修改web服务的启动命令,加入sshd启动:
    command: >
      sh -c "service ssh start && uwsgi --ini uwsgi.ini"
    
  3. 验证配置:确保sshd_config中ListenAddress 0.0.0.0、PermitRootLogin yes等配置正确,允许外部连接。

内容的提问来源于stack exchange,提问作者Nijo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 01:31:35