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

Docker化Django项目调整静态文件权限后出现Gunicorn OOM问题求助

Docker部署Django应用:修改静态文件夹权限后Gunicorn报OOM错误排查求助

我用Docker部署了一个Django Web应用,之前遇到静态文件无法被应用找到的问题,误以为是文件权限错误,于是把当前用户组添加到根目录下的静态文件夹权限中,之后应用就无法正常运行了。

具体表现:
现在Gunicorn会输出内存不足(OOM)相关信息,这个问题是修改根静态文件夹权限后才出现的。目前应用负载很低,因此我怀疑并非实际内存不足导致的。

Gunicorn日志:

[2024-08-30 19:08:12 +0000] [1] [INFO] Starting gunicorn 23.0.0
[2024-08-30 19:08:12 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1)
[2024-08-30 19:08:12 +0000] [1] [INFO] Using worker: sync
[2024-08-30 19:08:12 +0000] [7] [INFO] Booting worker with pid: 7
[2024-08-30 19:08:52 +0000] [1] [CRITICAL] WORKER TIMEOUT (pid:7)
[2024-08-30 19:08:53 +0000] [1] [ERROR] Worker (pid:7) was sent SIGKILL! Perhaps out of memory?
[2024-08-30 19:08:53 +0000] [8] [INFO] Booting worker with pid: 8

当前权限配置(ls -l输出):

-rw-rw-r-- 1 ash ash  757 Aug 30 20:17 docker-compose.yml
-rw-rw-r-- 1 ash ash  965 Aug 26 18:02 Dockerfile
drwxrwxr-x 7 ash ash 4096 Aug 26 18:32 gs-python
drwxr-xr-x 3 ash ash 4096 Aug 16 20:12 nginx
-rw-rw-r-- 1 ash ash    6 Aug 15 17:16 README.md
-rw-rw-r-- 1 ash ash   31 Aug 15 19:06 requirements.txt
drwxrwxrwx 2 ash ash 4096 Aug 30 19:50 static

相关配置文件:

docker-compose.yml

version: '3.8'

services:
  web:
    build: .
    command: gunicorn app-name.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - ./gs-python:/app
      - ./gs-python/static:/app/static  # Directly mount local static folder
    expose:
      - "8000"
    depends_on:
      - db

  db:
    image: postgres:13
    volumes:
      - postgres_data:/var/lib/postgresql/data
    environment:
      POSTGRES_DB: 
      POSTGRES_USER: 
      POSTGRES_PASSWORD: 

  nginx:
    image: nginx:latest
    volumes:
      - ./static:/app/static  
      - ./nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf
    ports:
      - "8001:80"
    depends_on:
      - web

volumes:
  postgres_data:

Dockerfile

# Step 1: Use an official Python runtime as a parent image
FROM python:3.9-slim

# Step 2: Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

# Step 3: Set up working directory
WORKDIR /app

# Step 4: Install system dependencies
RUN apt-get update && apt-get install -y \
    python3-dev \
    libpq-dev \
    postgresql-client \
    build-essential \
    nginx \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Step 5: Install Python dependencies
COPY requirements.txt /app/
RUN pip install --no-cache-dir -r requirements.txt

# Step 6: Copy project files
COPY . /app/

# Step 7: Set up database 
# RUN python manage.py makemigrations
# RUN python manage.py migrate

# Step 8: Collect static files
RUN python gs-python/manage.py collectstatic --noinput

# Step 9: Gunicorn command to run the app
CMD ["gunicorn", "--bind", "0.0.0.0:8000", "app-name.wsgi:application"]

nginx default.conf

server {
    listen 80;
    
    location / {
        proxy_pass http://web:8000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
    }

    location /static/ {
        alias /app/static/; 
        autoindex on;
    }
}

目录结构说明:
我的目录结构中存在两个static文件夹,不清楚具体生成时机,大概率是配置静态文件时产生的,目录结构截图如下:
目录结构截图

现在寻求帮助排查问题,解决应用无法运行的状况。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 23:00:04