1. Write python flask code, simply write an addition interface, named sum.py
import json
from flask import Flask,request,render_template
app = Flask(__name__)
@app.route('/')
def index():
return 'hello world'
@app.route('/sum',methods=['POST'])
def correct():
a= request.json['a']
b=request.json['b']
sum=int(a)+int(b)
print(sum)
result={"sum:":sum}
return result
if __name__ == '__main__':
app.run(host="0.0.0.0",port=5000)
2. The prerequisite for packaging into a mirror image is to write out what dependencies are required. Here we recommend pipreqs
--Run the command pip install pipreqs (if not installed)
--Run command pipreqs ./ --encoding=utf8 --force
You can see that requirements.txt is generated in the directory
3. Write dockerfile
FROM python:3.7
COPY . /app/
RUN pip install -r /app/requirements.txt
WORKDIR /app
EXPOSE 5000
CMD [ "python","sum.py" ]
After executing the first three steps, the entire code directory structure is shown in the figure
4. Package image
-- Execute the command docker build -f Dockerfile -t pyhonflask .
After running, you can use the docker images command to view the packaged image
5. Run the image
Here I use docker desktop to run directly
View the status of docker startup
6. Verification interface