使用 docker compose 发布 Dockerise Django Cuda 应用程序

issue Dockerise Django Cuda application using docker compose

我正在尝试 dockerize 一个 Django Cuda 应用程序,该应用程序 运行 在 Nginx 上 Gunicorn.Problem 是我进行预测的时候..我得到一个错误 cuda drivers not找到

我的Docker文件:

FROM nvidia/cuda

FROM python:3.6.8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
WORKDIR /app
COPY ./requirements.txt /app/requirements.txt
RUN python -m pip install --upgrade pip
RUN pip install cmake
RUN pip install opencv-python==4.2.0.32
# RUN pip install pywin32==227
RUN pip install -r requirements.txt
COPY . /app
RUN python manage.py collectstatic --noinput
RUN pip install gunicorn
RUN mkdir -p /home/app/staticfiles/

Ngnix Docker文件

FROM nginx:1.21-alpine
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d

Ngnix 配置文件

upstream project_settings {
    server web:8000;
}

server {

    listen 80;
    client_max_body_size 0;

    location / {
        proxy_pass http://project_settings;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_redirect off;
    }
    location /static/ {
        alias /home/app/staticfiles/;
    }
}

主要Docker撰写文件

services:
  nginx:
    build: ./nginx
    ports:
      - 1300:80
    volumes:
      - static_volume:/home/app/staticfiles/
    depends_on:
      - web
  web:
    build: .
    command: gunicorn project_settings.wsgi:application --bind 0.0.0.0:8000
    volumes:
      - static_volume:/home/app/staticfiles/
    image: sampleapp1121asa
    expose:
      - 8000
    deploy:
      resources:
        reservations:
          devices:
            - capabilities: [ gpu ]
volumes:
  static_volume:

使用 docker compose 时无法正常工作,当我尝试单独构建 docker 文件然后使用 docker run --rm --gpus all -p 8000:8000 deefakedetectiondockerimage python3 manage.py runserver 0.0.0.0:8000 运行 时它可以工作,但问题在于此方法是我不能在 docker 中提供静态文件。需要 Ngnix 来提供静态文件,这意味着我需要 运行 通过 docker 仅撰写

我找到了相同的解决方案。实际上,当您尝试在单个容器中 运行 多个图像时,运行 从 docker-compose 变得困难。

所以我使用 DockerFile 为应用程序构建镜像,为 Ngnix 构建单独的镜像,并启用两个容器与 unix 套接字连接的通信。

我更新的docker申请文件:

#pull the nvidia cuda GPU docker image
FROM nvidia/cuda

#pull python 3.6.8 docker image
FROM python:3.6.8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
#create a directory to serve static files 
RUN mkdir -p /home/app/staticfiles/app/uploaded_videos/
WORKDIR /app
COPY ./requirements.txt /app/requirements.txt
RUN python -m pip install --upgrade pip
RUN pip install cmake
RUN pip install opencv-python==4.2.0.32
RUN pip install -r requirements.txt
COPY . /app
RUN python manage.py collectstatic --noinput
RUN pip install gunicorn
RUN mkdir -p /app/uploaded_videos/app/uploaded_videos/

VOLUME /app/run/
ENTRYPOINT ["/app/bin/gunicorn_start.sh"]

gunicorn_start.sh 脚本

#!/bin/bash

NAME="project_settings"                                  # Name of the application
DJANGODIR=/app             # Django project directory
SOCKFILE=/app/run/gunicorn.sock  # we will communicte using this unix socket
NUM_WORKERS=3                                     # how many worker processes should Gunicorn spawn
DJANGO_SETTINGS_MODULE=project_settings.settings             # which settings file should Django use
DJANGO_WSGI_MODULE=project_settings.wsgi                     # WSGI module name

echo "Starting $NAME as `whoami`"

# Create the run directory if it doesn't exist
RUNDIR=$(dirname $SOCKFILE)
test -d $RUNDIR || mkdir -p $RUNDIR

# Start your Django Gunicorn
 gunicorn project_settings.wsgi:application --bind=unix:$SOCKFILE --workers $NUM_WORKERS --timeout 600

我更新的 docker Nginx 文件

FROM nginx
WORKDIR /etc/nginx/
RUN rm /etc/nginx/conf.d/default.conf
COPY nginx.conf /etc/nginx/conf.d
EXPOSE 80

对于一步一步的过程,你可以按照这个 blog