基本环境:
Ubuntu 16.10
docker 17.06.0-ce
压缩自己的项目文件
1.这里需要注意的是,在压缩的时候,也需要把自己的需要的Python包写在requirement.txt,这样搭建环境的时候才会知道你需要什么包,才能一起安装,我的项目的requirement.txt 内容如下。
Django==1.10.5
rpy2==2.8.5
PyMySQL==0.7.9
numpy==1.13.0
pandas==0.20.2
scikit-learn==0.18.1
scipy==0.19.0
uwsgi
2.更改自己的settings.py,为Nginx服务器代理设置部署环境
更改的内容如下
STATIC_ROOT = os.path.join(BASE_DIR, 'collect_static')
ALLOWED_HOST = ['*']
注意:这里不能将部署的环境与开发环境的静态文件相同,否则会引起错误。
修改好后运行如下命令:
python manage.py collectstatic
这个时候就可以安心压缩文件了
tar czvf FileName.tar DirName
构建web环境
1.将打包好的项目文件放入web文件夹中
cp /home/haoyu/AAPlatform.tar .
2.编写Dockerfile
这里主要是搭建基础环境
# 基础镜像
FROM ubuntu:16.10
FROM daocloud.io/python:3.5
# 维护者信息
MAINTAINER haoyu <why_oneisall@163.com>
ADD AAPlatform.tar /usr/src/
# app 所在目录
WORKDIR /usr/src/AAPlatform
ENV LD_LIBRARY_PATH=/usr/local/lib
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 40976EAF437D05B5
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 3B4FE6ACC0B21F32
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety main restricted" > /etc/apt/sources.list
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-updates main restricted" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety universe" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-updates universe" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety multiverse" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-updates multiverse" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-backports main restricted universe multiverse" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-security main restricted" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-security universe" >> /etc/apt/sources.list
RUN echo "deb http://mirrors.tuna.tsinghua.edu.cn/ubuntu/ yakkety-security multiverse" >> /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y make
RUN echo "deb http://cran.rstudio.com/bin/linux/ubuntu yakkety/" >> /etc/apt/sources.list
RUN apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 51716619E084DAB9
RUN apt-get update
RUN apt-get install -y r-base # r-base-dev r-cran-mgcv r-base-core r-base-html r-recommended
# 安装 app 所需依赖
RUN pip install --no-cache-dir -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
基本上Django和R搭建的环境都在这了。
2.编写start.sh
这里是要把docker的Django运行起来
内容如下:
#!/bin/bash
#
docker build -t aaplatform/django-app .
docker run --name django \
-v /usr/src/AAPlatform \
-v /usr/src/AAPlatform/collect_static \
-p 12000:8000 \
-d feiyu/django-app /usr/local/bin/uwsgi --http :8000 --chdir /usr/src/AAPlatform -w AAPlatform.wsgi
#-d aaplatform/django-app /usr/local/bin/gunicorn myblog.wsgi:application -w 1 -b :8000
搭建Nginx环境
1.编写Dockerfile文件
配置Nginx相关的环境
FROM daocloud.io/nginx
MAINTAINER haoyu <www.haoyu.com>
RUN rm /etc/nginx/conf.d/default.conf
ADD nginx-conf/ /etc/nginx/conf.d/
2.编写Server配置文件
需要将系统默认的配置文件更改一下
在nginx-conf中:
server {
listen 80;
server_name localhost;
charset utf-8;
root /usr/src/AAPlatform;
access_log /var/log/nginx/django.log;
location ^~ /static {
alias /usr/src/AAPlatform/collect_static;
}
location / {
proxy_pass http://web:8000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
保证Nginx环境能够构建起来。
3.编写start.sh
让docker运行起来
#!/bin/bash
#
docker build -t nginx .
docker run --name nginx-server \
--link django:web \
-v /www/collect_static \
--volumes-from django \
-p 8888:80 \
-d nginx
整个环境结合
1.构建docker-start.sh
启动web和Nginx
#!/bin/bash
#
cd ./web
echo "start web --------------------------"
./start.sh
cd ../nginx
echo "start nginx-------------------------"
./start.sh
2.编写stop.sh
关闭docker
#/bin/bash
#
sudo docker kill $(docker ps -a -q)
sudo docker rm $(docker ps -a -q)
运行测试
1.运行sh文件
$ ./docker-start.sh
2.访问网站
http://127.0.0.1:8888/login
注意:要访问的是Nginx服务器。
将部署好的镜像文件导出
1.docker save
docker save 00273588addb -o aaplatform.tar aaplatform/django-app:latest
2.docker load
docker load < aaplatform.tar