1.新建Dockerfile
FROM python:3.8.5 MAINTAINER ChsterChen ENV PYTHONUNBUFFERED 1 COPY pip.conf /root/.pip/pip.conf RUN mkdir -p /var/www/html/student_api WORKDIR /var/www/html/student_api ADD . /var/www/html/student_api RUN pip install -r requirements.txt RUN chmod a rwx -R /var/www/html/student_api/ RUN pwd VOLUME ["/tmp"] EXPOSE 8000 ENTRYPOINT ["uwsgi", "--ini", "uwsgi-docker.ini"] # RUN python manage.py collectstatic --noinput]
2.新建pip.conf中国镜像源
[global] index-url = https://mirrors.aliyun.com/pypi/simple/ [install] trusted-host=mirrors.aliyun.com
3.生成requirement.txt
pip3 freeze > requirements.txt
4.新建uwsgi-docker.ini环境变量
[uwsgi] project=student_api uid=www-data gid=www-data base=/var/www/html chdir=%(base)/%(project) module=signup_api.wsgi:application master=True processes=2 http=0.0.0.0:8000 #这儿立即应用uwsgi做web服务器,应用http。假如应用nginx ,必须应用socket沟通交流 。 buffer-size=65536 pidfile=/tmp/%(project)-master.pid vacuum=True max-requests=5000 # daemonize=/tmp/%(project)-uwsgi.log #注解掉,要不然变成后台进程,器皿会立即撤出 #设定一个要求的请求超时時间(秒) ,假如一个要求超出了这一時间,则要求被丢掉 harakiri=60 #当一个要求被harakiri干掉会,会輸出一条系统日志 harakiri-verbose=true static-map = /static=%(base)/%(project)/static
5.生成镜像系统
sudo docker build -t student_api:v1 .
6.运作器皿
sudo docker run -v /docker/volume:/tmp -it -d --name student_api -p 8001:8000 student_api:v1
7.api文档等前端开发静态数据文档没法根据uwsgi浏览到 ,需根据python ./manage.py collectstatic把全部静态数据資源生成到STATIC_ROOT文件夹名称内 https://note.qidong.name/2017/07/uwsgi-serve-django-static/
先settings.py中改动
STATIC_ROOT = '你的新项目文件目录/static'
随后根据下列指令把静态数据文档生成到STATIC_ROOT文件目录
python ./manage.py collectstatic
uwsgi环境变量中增加静态数据文档投射配备
static-map = /static=%(base)/%(project)/static
再次build镜像系统 运作器皿
8.nginx配备
location / { proxy_set_header Host $http_host; #关键,危害到api文档的一切正常访问 proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-NginX-Proxy true; proxy_pass http://127.0.0.1:8001/; }