1.新建Dockerfilehtml
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.txtpython
pip3 freeze > requirements.txt
4.新建uwsgi-docker.ini配置文件nginx
[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.生成鏡像web
sudo docker build -t student_api:v1 .
6.運行容器docker
sudo docker run -v /docker/volume:/tmp -it -d --name student_api -p 8001:8000 student_api:v1
7.接口文檔等前端靜態文件沒法經過uwsgi訪問到,需經過python ./manage.py collectstatic把全部靜態資源生成到STATIC_ROOT文件夾內 https://note.qidong.name/2017/07/uwsgi-serve-django-static/django
先settings.py中修改api
STATIC_ROOT = '你的項目目錄/static'
而後經過如下命令把靜態文件生成到STATIC_ROOT目錄服務器
python ./manage.py collectstatic
uwsgi配置文件中新增靜態文件映射配置app
static-map = /static=%(base)/%(project)/static
從新build鏡像+運行容器
8.nginx配置
location / { proxy_set_header Host $http_host; #重要,影響到接口文檔的正常瀏覽 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/; }