flask后台部署套路(三)
FROM myflask:1.0
RUN pip install gunicorn
ENV start_param index:app
WORKDIR /app
CMD gunicorn -w 2 -b 0.0.0.0:5000 $start_param
上节课我们写好的做好的容器,大部分的启动参数是写死的包括我们的启动进程数 端口号这些,这次我们通过配置文件的方式,来把这些参数做一个参数化。先来看一个简单的配置文件的文件内容
(配置连接)[http://docs.gunicorn.org/en/stable/settings.html#settings]
workers = 2
bind='0.0.0.0:5001'
timeot=60
daemon = True
accesslog='./access.log'
errorlog='./error.log'
做一个解释
1行 进程数 2行 绑定的ip和端口 3行超时时间 4 是否以后台还是前台运行 5 日志
6 错误日志
5-6两个文件一般也要映射到外部目录
- 老规矩测试容器先试试
docker exec -it myapp sh
- 退出到根目录
cd ..
- 创建一个gunicorn.conf文件
vi gunicorn.conf
写入内容
workers = 2
bind='0.0.0.0:5001'
timeot=60
daemon = True
accesslog='./access.log'
errorlog='./error.log'
ps: 插入冷知识
ps -ef | grep gunicorn
1 root 0:00 /bin/sh -c gunicorn -w 2 -b 0.0.0.0:5000 $start_param
7 root 0:00 {gunicorn} /usr/local/bin/python /usr/local/bin/gunicorn -w 2 -b 0.0.0.0:5000 mytest:myapp
10 root 0:00 {gunicorn} /usr/local/bin/python /usr/local/bin/gunicorn -w 2 -b 0.0.0.0:5000 mytest:myapp
11 root 0:00 {gunicorn} /usr/local/bin/python /usr/local/bin/gunicorn -w 2 -b 0.0.0.0:5000 mytest:myapp
23 root 0:00 grep gunicorn
第二行是gunicorn的主进程开出了第三四两行子进程,第一行可以看到我们运行的那个指令
3. 进入/app
4. 使用配置文件的方式来运行这个运行程序
gunicorn -c /gunicorn.conf mytest:myapp
- 再次查看进程
ps -ef | grep gunicorn
1 root 0:00 /bin/sh -c gunicorn -w 2 -b 0.0.0.0:5000 $start_param
7 root 0:00 {gunicorn} /usr/local/bin/python /usr/local/bin/gunicorn -w 2 -b 0.0.0.0:5000 mytest:myapp
10 root 0:00 {gunicorn} /usr/local/bin/python /usr/local/bin/gunicorn -w 2 -b 0.0.0.0:5000 mytest:myapp
11 root 0:00 {gunicorn} /usr/local/bin/python /usr/local/bin/gunicorn -w 2 -b 0.0.0.0:5000 mytest:myapp
28 root 0:00 {gunicorn} /usr/local/bin/python /usr/local/bin/gunicorn -c /gunicorn.conf mytest:myapp
31 root 0:00 {gunicorn} /usr/local/bin/python /usr/local/bin/gunicorn -c /gunicorn.conf mytest:myapp
32 root 0:00 {gunicorn} /usr/local/bin/python /usr/local/bin/gunicorn -c /gunicorn.conf mytest:myapp
34 root 0:00 grep gunicorn
可以看出来直接就运行了并且出了一个主进程,并且派生了另外两个子进程。
先kill掉这个主进程
kill 28
- 接下来想要容器启动时候就能接受自定义的参数
写一个脚本内容如下(这是拓展知识)
#!/bin/sh
echo $*
这是一个简单的脚本,可以用了输出执行脚本时所有的参数
7.来到根目录下,创建一个test文件
vi test
#!/bin/sh
echo $*
8.给这个test文件加一个可执行的权限
chmod +x test
注意加完之后文件的文件名会变成绿色
9. 现在来做个实验,输入
./test -a abc -b xiong -p root
- 如果我们现在这份脚本是
#!/bin/sh
gunicorn $*
这样我们就可以直接通过脚本来启动
- 现在开始整合整体成一个新的镜像
退出容器
ctrl+D
- 在
~
目录下
mkdir conf
- 进入conf
cd conf
- 创建一个配置文件
vi gunicorn.conf
workers = 1
bind='0.0.0.0:5001'
timeot=60
accesslog='./access.log'
errorlog='./error.log'
删除掉daemon = True,让他前台运行,否则容器到时候会关闭
4. 退出
cd ..
- 在
~
目录下
mkdir flask1.2
且进入
6. 创建两个文件 第一个startup.sh
#!/bin/sh
gunicorn $*
- 创建Dockerfile
FROM myflask:1.1
COPY ./startup.sh /
RUN chmod +x /startup.sh
ENTRYPOINT ["/startup.sh"]
2行把当前目录下的startup.sh
拷贝到/
目录下 3行 设置可执行权限 4行
这是时候我们执行docker run abc
后面接任何的命令都会在这些命令的前面补上/startup.sh
,还搞不懂的反复看看上文的测试环境下的实验
8. 构建镜像
docker build -t myflask:1.2 .
- 开始启动
docker run --name myapp2 -d -p 8082:5000 -v /root/myapp:/app -v /root/conf/gunicorn.conf:/gunicorn.conf myflask:1.2 -w 2 -c /gunicorn.conf mytest:myapp
特别注意 指令里面同样的命令是可以覆盖配置文件的