第一步:先写好flask最小化代码,保证可以访问的到
[root@VM-0-15-centos Dockerfile]# cat flaskapp.py from flask import Flask import os app = Flask(__name__) @app.route('/') def index(): res = os.environ.get('name') return "hello,{}".format(res) app.run(host='0.0.0.0',port=5000)
第二步:编写dockerfile代码
[root@VM-0-15-centos Dockerfile]# vim Dockerfile FROM python:3.6 MAINTAINER <fengzi@163.com> ADD flaskapp.py /root RUN pip install jinja2 -i https://mirrors.aliyun.com/pypi/simple RUN pip install flask -i https://mirrors.aliyun.com/pypi/simple ENV name=fengzi EXPOSE 5000 CMD ["python","/root/flaskapp.py"]
第三步:这时候你可以用docker run启动,我这里用k8s启动,所以还需要写yaml文件,这里我往环境变量里传入了name=fengzi变量,因此在访问web的时候就会是hello,fengzi,按照这个思路你可以传入你想传入的变量
[root@VM-0-15-centos ~]# cat flaskapp1.yaml apiVersion: v1 kind: Service metadata: name: flaskapp1 labels: app: flaskapp1 spec: selector: app: flaskapp1 ports: - name: http port: 5000 --- apiVersion: apps/v1 kind: Deployment metadata: name: flaskapp1 spec: replicas: 1 selector: matchLabels: app: flaskapp1 template: metadata: labels: app: flaskapp1 version: v1 spec: containers: - name: flaskapp-container image: mysite imagePullPolicy: IfNotPresent env: - name: name value: fengzi
第四步:创建pod
[root@VM-0-15-centos ~]# kubectl apply -f flaskapp1.yaml service/flaskapp1 configured deployment.apps/flaskapp1 configured
第五步:验证