Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implementation steps for Docker to deploy Flask applications

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

I. purpose

Write a Flask application locally, encapsulate it with Docker, upload it to your own server, and complete the deployment.

Flow chart:

Second, the experimental environment

Local: Windows10 1909

Server: Aliyun Centos system

III. Required software

1.DockerDesktop

2.Pycharm 2020.3.3

IV. Steps

1. Write the Flask application locally

(1) create a Flask application docker_flask in PyCharm

(2) install gunicorn,gevent package

(3) create a new gunicorn.config.py file and enter the following

Workers = 5 # defines the number of processes that are open to process requests at the same time, and adjusts worker_class = "gevent" # adopts gevent library, supports asynchronous processing of requests, and improves throughput bind = "0.0.0.0 gevent 8080" # where 8080 can be adjusted at will

(4) create a new requirements.txt file and enter the following

Flask

Gunicorn

Gevent

(5) create a Dockerfile file and fill in the following

FROM python:3.7WORKDIR / usr/src/app COPY requirements.txt. / RUN pip install-r requirements.txt-I COPY. . CMD ["gunicorn", "app:app", "- c", ". / gunicorn.conf.py"] # the previous app is the file name started by python, that is, the one after app.py; is the name of the pre-launched application in the flask project

(6) Project structure

two。 Build a Docker image

1. Enter the directory of this project

two。 To build an image, enter the following command

Docker build-t 'docker_flask'.

Then the six-step operation will be performed. Let's just look at the results without worrying about it.

Check it out

All right, at this point, our self-made mirror image is ready, and we can run locally and experiment.

3. Upload the image to Ali Cloud Warehouse

(1) create your own Docker repository on Aliyun.

(2) upload image

1. Log in to Aliyun Docker Registry and remember to change your user name to your own.

Docker login-- username= your Aliyun user name registry.cn-hangzhou.aliyuncs.com

Login password is your Aliyun login password.

two。 Enter the following two lines of command to push the image to Registry

Docker tag [ID of image] registry.cn-hangzhou.aliyuncs.com/ namespace / repository name: [image version number] docker push registry.cn-hangzhou.aliyuncs.com/ namespace / warehouse name: [image version number]

Then don't worry about it, just let it run on its own.

I uploaded it here before.

4. Pull the image on the server and run it (of course, you have to secure the docker on the server first)

Docker pull registry.cn-hangzhou.aliyuncs.com/ Namespace / Warehouse name: [image version number]

Ssh logs in to the server, enter the above command, and then check to see if it has been pulled down

4. Run it and see if it works.

Here I map port 8080 of docker to port 28080 of the server.

All right, enter the server ip:28080 and see if you can see the Hello World! (remember to open the port)

So far, this is the end of this article on the implementation steps of Docker deploying Flask applications. For more information about Docker deploying Flask applications, please search the previous articles or continue to browse the relevant articles below. I hope you will support it in the future!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report