问题Dockerise Django Cuda应用程序使用docker撰写



我正在尝试dockerize在Nginx和Gunicorn上运行的Django Cuda应用程序。问题是当我去做预测时…我得到一个错误,cuda驱动程序没有找到

我DockerFile:

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 DockerFile

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组成工作,当我尝试单独构建dockerfile,然后使用docker run --rm --gpus all -p 8000:8000 deefakedetectiondockerimage python3 manage.py runserver 0.0.0.0:8000运行它的工作,但这种方法的问题是我不能在docker中提供静态文件。nnix需要提供静态文件,这意味着我需要通过docker compose只运行

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

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

我更新的dockerfile for application:

#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

我更新的Nginx docker文件

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

关于一步一步的过程,你可以关注这个博客

最新更新