Home > Article > Backend Development > What is the method for packaging the python flask project into a docker image for release?
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. To package it into a mirror, you must write out what dependencies are needed. Pipreqs
is recommended here--Run the command pip install pipreqs (if not installed)
--Run the command pipreqs ./ --encoding=utf8 --force
You can see that requirements are generated in the directory .txt
3. Write the 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 as shown in the figure
4. Packaging image
--Execute the command docker build -f Dockerfile -t pyhonflask .
Available after the operation is completed Docker images command to view the packaged image
## 5. Run the imageI used docker desktop to run it directly Check docker startup status 6. Verification interfaceThe above is the detailed content of What is the method for packaging the python flask project into a docker image for release?. For more information, please follow other related articles on the PHP Chinese website!