在python中安装应用

1
pip3 isntall gunicorn gevent

在Django项目中的setting.py中配置相应的配置

1
2
3
4
5
6
7
8
9
10
11
12
13
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
'rest_framework',
'corsheaders',
'dwebsocket',
'gunicorn',
]

然后在Django项目的根目录编写gunicorn的配置文件:gunicorn.conf.py

1
2
3
4
import multiprocessing

bind = "0.0.0.0:8000" #绑定的ip与端口
workers = 1 #进程数

这里注意一点,ip必须是0.0.0.0,不要写成127.0.0.1,否则外部环境会访问不到容器内的服务

接下来在项目的根目录编写好依赖列表:requirements.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Django==2.0.4
django-cors-headers==2.5.3
djangorestframework==3.9.3
celery==4.4.2
dwebsocket==0.5.12
redis==3.3.11
pymongo==3.8.0
PyMySQL
Pillow
pyjwt
pycryptodome
selenium
qiniu
gunicorn
gevent

在根目录编写DockerFile文件:

1
2
3
4
5
6
7
8
9
10
FROM python:3.7
WORKDIR /Project/day3

COPY requirements.txt ./
RUN pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple

COPY . .
ENV LANG C.UTF-8

CMD ["gunicorn", "day3.wsgi:application","-c","./gunicorn.conf.py"]

本次的基础镜像我们选择3.7,将文件上传到Docker

对项目进行打包

要进入到项目的根目录下执行

1
docker build -t 项目名称 .

第一次会很慢,耐心等待

启动镜像服务

1
docker run -it --rm -p 5000:8000 项目名称

进行Vue打包,配置文件config/index.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
build: {
// Template for index.html
index: path.resolve(__dirname, '../dist/index.html'),

// Paths
assetsRoot: path.resolve(__dirname, '../dist'),
assetsSubDirectory: 'static',
assetsPublicPath: './',

/**
* Source Maps
*/

productionSourceMap: true,
// https://webpack.js.org/configuration/devtool/#production
devtool: '#source-map',

// Gzip off by default as many popular static hosts such as
// Surge or Netlify already gzip all static assets for you.
// Before setting to `true`, make sure to:
// npm install --save-dev compression-webpack-plugin
productionGzip: false,
productionGzipExtensions: ['js', 'css'],

// Run the build command with an extra argument to
// View the bundle analyzer report after build finishes:
// `npm run build --report`
// Set to `true` or `false` to always turn it on or off
bundleAnalyzerReport: process.env.npm_config_report
}
}

将打包目录改成相对路径,同时注意路由的配置,如果曾经修改为history模式记得改回hash

1
2
3
4
export default new Router({
routes:routes,
//mode:'history' /*hash*/
})

在根目录进入终端

1
npm install build

在根目录下编辑dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#FROM node:lts-alpine
#
## install simple http server for serving static content
#RUN npm install -g http-server
#
#
#COPY ./dist ./dist
#
#
#EXPOSE 8080
#CMD [ "http-server", "dist" ]

FROM nginx
COPY dist/ /usr/share/nginx/html/
COPY default.conf /etc/nginx/conf.d/default.conf

将项目传入Docker

进行打包操作

1
docker build -t 项目名 .

运行镜像

1
docker run -it --rm -p 8081:8080 项目名