Home  >  Article  >  Backend Development  >  What is the method for packaging the python flask project into a docker image for release?

What is the method for packaging the python flask project into a docker image for release?

王林
王林forward
2023-04-28 15:07:131389browse

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

What is the method for packaging the python flask project into a docker image for release?

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

What is the method for packaging the python flask project into a docker image for release?

4. Packaging image

--Execute the command docker build -f Dockerfile -t pyhonflask .

What is the method for packaging the python flask project into a docker image for release?

Available after the operation is completed Docker images command to view the packaged image

What is the method for packaging the python flask project into a docker image for release?

## 5. Run the image

I used docker desktop to run it directly

What is the method for packaging the python flask project into a docker image for release?

Check docker startup status

What is the method for packaging the python flask project into a docker image for release?

6. Verification interface

What is the method for packaging the python flask project into a docker image for release?

The 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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete