搭建适合自己的flask代码运行的docker环境【uwsgi版】
程序员文章站
2022-03-19 23:43:23
...
1 根据官网centos7的docker image生成uwsgi image
(1)在docker-host上建立,一个目录uwsig,进入目录新建一个dockerfile,内容如下
###该dockerfile文件,是基于官方的centos7的image,生成uwsgi的image
FROM docker.io/centos:7
RUN ["/bin/bash", "-c", " \
yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm \
MySQL-python.x86_64 gcc python-devel pcre-devel openssl-devel \
&& yum -y install python-pip \
&& yum clean all \
&& pip install flask flask-wtf flask-bootstrap flask-script flask_uploads uwsgi"]
(2)根据dockerfile生成uwsgi image
build image built -f dockerfile -t caowencomeon/uwsgi:1.0 ./
(3)确认uwsgi image创建成功
docker image ls
2 flask项目代码框架
(1)flask项目框架:
myapp:文件内为项目源代码,
manage.py:为运行项目源代码的入口,
uwsgi.ini:为uwsgi服务器运行需要的配置文件,
dockerfile.ini:用来个性化定制本项目运行所需要的docker环境。
(2)dockerfile.ini文件内容
FROM caowencomeon/uwsgi:1.0
COPY src /testbed/src
COPY uwsgi.ini /testbed/uwsgi.ini
VOLUME /var/log
VOLUME /data
EXPOSE 5000
CMD ["uwsgi", "/testbed/uwsgi.ini"]
(3)uwsgi.ini配置文件内容
[uwsgi]
http = 0.0.0.0:5000
chdir = ./testbed/src
wsgi-file = manage.py
callable = app
processes = 4
threads = 2
stats = 127.0.0.1:5050
(4)可独立运行,也可被uwsgi.ini调用的manage.py文件内容
#!/usr/bin/python
# coding:utf-8
from flask_script import Manager
from myapp import app
manager = Manager(app)
if __name__ == '__main__':
manager.run()
3 根据uwsgi image以及dockerfile.ini运行一个uwsgi container
(1)复制整个flask代码框架到docker-host上
(2)定制本项目的docker环境
[aaa@qq.com testbed]# cd testbed/
[aaa@qq.com testbed]# docker build -f dockerfile.ini -t myproject:uwsgi ./
[aaa@qq.com testbed]# docker image ls
(3)运行容器
[aaa@qq.com testbed]# docker container run -d -p 5000:5000 --name myproject myproject:uwsgi
[aaa@qq.com testbed]# docker container ls
上一篇: nginx的反向代理