利用Docker部署Django

最近在部署Django,真的是太他们复杂了,nginx + uwsgi3 + python3 + django2,这一串部署完真是分分钟奔溃啊。
教程

docker部署

默认你已经安装好 docker

dockerfile

抽取一个python3.6,之后机会为你准备好一个python的环境

            "PYTHON_VERSION=3.6.9",
            "PYTHON_PIP_VERSION=19.2.2",

编写Dockerfile文件
之后

FROM python:3.6
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
COPY pip.conf /root/.pip/pip.conf
COPY requirements.txt /usr/src/app/
RUN pip install -r /usr/src/app/requirements.txt
RUN rm -rf /usr/src/app
COPY . /usr/src/app
CMD [ "python", "./manage.py", "runserver", "0.0.0.0:8080"]

pip conf

这里是为了使用镜像 pip install 相关依赖,速度快的起飞

[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host=mirrors.aliyun.com

requirements.txt

pip3 freeze > requirements.txt #生成requirements.txt文件

上传项目到云服务器

这里强调一点:虚拟环境不必加进去
因为 docker 会自动去下载依赖
而且虚拟环境占用 100M+- 的空间,上传的时间够等 5+ 分钟
这里程序主目录,Dockerfile,requirements.txt,pip.conf 都放在同一个目录即可
Xshell_2019-08-20_16-02-08.png

启动服务

生成镜像:到了项目和 dockerfile 同一级的目录,也就是上图的目录

(注意后面的 . 不要省略)

docker build -t my-python-app .

开启容器:

这里的 8080 由上面的 dockerfile 决定

docker run -d  --name django_web -p 8080:8080 django_web

其他

ALLOWED_HOSTS = ['*']

pycharm64_2019-08-20_16-03-30.png


挂载一个vloume

在上面的基础上修改一下,增加一个挂载volume的目录,方便平时代码更新

FROM python:3.6
RUN mkdir -p /usr/src/notice/docker
RUN mkdir -p /usr/src/notice/app
COPY pip.conf /root/.pip/pip.conf
COPY requirements.txt /usr/src/notice/docker
RUN pip install -r /usr/src/notice/docker/requirements.txt
WORKDIR /usr/src/notice/app
CMD [ "python", "./manage.py", "runserver", "0.0.0.0:8080"]

构建并运行docker

docker build -t django_web .
docker run -d  --name django_web -v /root/py_docker/app:/usr/src/notice/app -p 8080:8080 django_web

之后随时提交的代码更改可以立即执行
不过存在的问题是,如果模块变更的话,需要重新构建。

Nginx反向代理

这个利用宝塔或者lnmp的反向代理很好配置,也很好的处理ssl
chrome_ADpJZe4Rc8.png

部署mysql和phpmydadmin

部署mysql

docker run --name my-own-mysql -e MYSQL_ROOT_PASSWORD=mypass123 -d mysql:8.0.1

The option --name allows us to assign a specific name for our running container.
The option -e is used to pass a value for the container environment variable MYSQL_ROOT_PASSWORD. This variable is requested by the image to run properly and it will be assigned to the root password of MySQL. More information about this image can be found in docker hub (here).
The option -d means that docker will run the container in the background in “detached” mode. If -d is not used the container run in the default foreground mode.
Finally, we need to indicate docker to use the image mysql:8.0.1 just downloaded, to run the container.

部署phpmyadmin

docker run --name my-own-phpmyadmin -d --link my-own-mysql:db -p 8081:80 phpmyadmin/phpmyadmin

The options name and d has been explained in the previous section.
The option --link provides access to another container running in the host. In our case the container is the one created in the previous section, called my-own-mysql and the resource accessed is the MySQL db.
The mapping between the host ports and the container ports is done using the option -p followed by the port number of the host (8081) that will be redirected to the port number of the container (80, where the ngix server with the phpMyAdmin web app is installed).
Finally, the docker run command needs the image used to create the container, so we will use the phpmyadmin image just pulled from docker hub.

chrome_iYmIoPFuOI.png

提交Github并自动构建

另外还有一种方案就是提交到github之后,自动构建并部署,不过貌似比较麻烦,我暂时也用不到就算啦。

参考:

Last modification:August 24th, 2019 at 12:18 am

Leave a Comment