一个用来快速部署 Django API 的 DockerFile

2022-10-26 14:34:02 浏览数 (1)

这是一个基于python3.7.1的用来快速发布Django项目的 DockerFile

功能:

  • 快速发布Django REST MySQL搭建的API

使用:

  1. 根据dockerFile build 镜像
代码语言:javascript复制
docker build -f ./dockerfile -t [命名空间]/[imagesName][:版本] .

或者直接下载

代码语言:javascript复制
sudo docker pull registry.cn-hangzhou.aliyuncs.com/junebao/django_rest_mysql:1.2.6
  1. 创造并开启容器
代码语言:javascript复制
 docker run -v /dataVolume/django/:/usr/local/django -p 8002:8000 -d junebao/drmi:1.2.6

参数解释:

  • -p: 端口映射,冒号前面是宿主机端口(对外暴露),后面是容器内服务端口,默认8000,可通过修改uwsgi.ini修改
  • -v: 设置数据卷,冒号前面是宿主机路径,后面是容器内项目路径(容器内路径是固定的),会同步容器内项目代码和日志,按上面的配置,宿主机日志文件路径为/dataVolume/django/logs/uwsgi.log,项目路径是/dataVolume/django/project部署项目时直接把manage.py那一层移动到project目录即可。
代码语言:javascript复制
# 这是一个用来快速发布 Django   REST API 的镜像,基于 python3.7.1
# 项目的发布仅仅使用了 uginx,所以如果Django项目中需要处理静态文件,请使用Nginx
# 该镜像只支持 MySQL 数据库
FROM python:3.7.1
MAINTAINER junebao<15364968962@163.com>

ENV PROJECT_PATH /usr/local/django
ENV PROJECT_NAME project

RUN apt-get update && apt-get install -y 
        gcc 
        gettext 
    --no-install-recommends && rm -rf /var/lib/apt/lists/*

# pip配置阿里云

RUN mkdir ~/.pip
RUN touch ~/.pip/pip.conf
RUN echo "[global]" > ~/.pip/pip.conf
RUN echo "index-url = http://mirrors.aliyun.com/pypi/simple/" >> ~/.pip/pip.conf
RUN echo "[install]" >> ~/.pip/pip.conf
RUN echo "trusted-host=mirrors.aliyun.com" >> ~/.pip/pip.conf

RUN pip install --upgrade pip

RUN pip install django==2.2.7

RUN pip install djangorestframework==3.9.4

RUN pip install mysqlclient

RUN pip install pytz==2019.1

RUN pip install sqlparse==0.3.0

RUN pip install django-cors-headers==3.2.0 

RUN pip install uwsgi

WORKDIR $PROJECT_PATH

RUN cd $PROJECT_PATH

# 生成一个project的django project

RUN django-admin startproject project

RUN mkdir $PROJECT_PATH/logs && touch $PROJECT_PATH/logs/uwsgi.log

# 建立uwsgi.ini文件

RUN touch $PROJECT_PATH/$PROJECT_NAME/uwsgi.ini

# TODO:往ini文件中写入相关配置

RUN echo "# junebao/DRMI uwsgi.ini" > $PROJECT_PATH/$PROJECT_NAME/uwsgi.ini
RUN echo "[uwsgi]" >> $PROJECT_PATH/$PROJECT_NAME/uwsgi.ini
RUN echo "http = :8000" >> $PROJECT_PATH/$PROJECT_NAME/uwsgi.ini
RUN echo chdir = $PROJECT_PATH/$PROJECT_NAME >> $PROJECT_PATH/$PROJECT_NAME/uwsgi.ini
RUN echo module = $PROJECT_NAME.wsgi >> $PROJECT_PATH/$PROJECT_NAME/uwsgi.ini
RUN echo logto = $PROJECT_PATH/logs/uwsgi.log >> $PROJECT_PATH/$PROJECT_NAME/uwsgi.ini
RUN echo "master = true" >> $PROJECT_PATH/$PROJECT_NAME/uwsgi.ini
RUN echo "processes = 4" >> $PROJECT_PATH/$PROJECT_NAME/uwsgi.ini
RUN echo "vacuum = true" >> $PROJECT_PATH/$PROJECT_NAME/uwsgi.ini

EXPOSE 8000

CMD /usr/local/bin/uwsgi --ini $PROJECT_PATH/$PROJECT_NAME/uwsgi.ini

0 人点赞