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

王林
Release: 2023-04-28 15:07:13
forward
1518 people have browsed it

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)
Copy after login

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" ]
Copy after login

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template