基于centos构建flask-restful基础镜像
原创
©著作权归作者所有:来自51CTO博客作者WEL测试的原创作品,请联系作者获取转载授权,否则将追究法律责任
Dockerfile
虽然使用已有的python镜像,习惯了centos操作系统一些命令,基于此,构建了基于centos的flask-restful基础镜像,该进行的Dockerfile内容如下:
FROM centos
LABEL maintainer
ENV TZ=Asia/Shanghai
#设置时区
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo '$TZ' > /etc/timezone
#安装python3依赖
RUN yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel wget vim -y
#下载安装包
RUN wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz \
&& tar -zxvf Python-3.7.0.tgz\
&& cd Python-3.7.0 \
&& ./configure \
&& make \
&& make install \
&& cd .. && rm -rf Python* \
&& ln -s /usr/local/bin/python3 /usr/bin/python \
&& ln -s /usr/local/bin/pip3 /usr/bin/pip \
&& pip install --upgrade pip \
&& pip install flask-restful
WORKDIR /app
ENTRYPOINT ["python"]
使用示例
示例代码api.py
from flask import Flask
from flask_restful import Resource, Api
app = Flask(__name__)
api = Api(app)
class HelloWorld(Resource):
def get(self):
return {'hello': 'world'}
api.add_resource(HelloWorld, '/')
if __name__ == '__main__':
app.run(host='0.0.0.0')
把api文件放入/root/tmp/目录下,执行下述命令:
docker run -it -p 5005:5000 -v /root/tmp/:/app weltest/flask-restful api.py
- -p:映射端口,容器内部默认端口为5000,映射到宿主机的是5005
- -v:把对应文件挂载到容器/app目录(对应的WORKDIR)
- api.py:为示例代码
运行情况如下:
界面展示如: