Docker deployment of Flask application implementation steps

Docker deployment of Flask application implementation steps

1. Purpose

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

flow chart:

2. Experimental Environment

Local: Windows 10 1909

Server: Alibaba Cloud Centos system

3. Required Software

1.Docker Desktop

2. Pycharm 2020.3.3

4. Steps

1. Complete the Flask application locally

(1) Create a new Flask application docker_flask in PyCharm

(2) Install gunicorn and gevent packages

(3) Create a new gunicorn.config.py file and fill in the following content

workers = 5 # Define the number of processes that are opened at the same time to handle requests, and adjust appropriately according to the website traffic worker_class = "gevent" # Use the gevent library to support asynchronous processing of requests and improve throughput bind = "0.0.0.0:8080" # Here 8080 can be adjusted at will

(4) Create a new requirements.txt file and fill in the following content

flask
gunicorn
gevent

(5) Create a Dockerfile file and fill in the following content

FROM python:3.7
WORKDIR /usr/src/app
 
COPY requirements.txt ./
RUN pip install -r requirements.txt -i 
 
COPY . .
 
CMD ["gunicorn", "app:app", "-c", "./gunicorn.conf.py"] #The first app is the file name started by Python, i.e. app.py; the second one is the pre-started application name in the flask project

(6) Project structure

2. Build a Docker image

1. Enter the directory of this project

2. Build the image and enter the following command

docker build -t 'docker_flask' .

Then six steps will be executed. We don't need to worry about it and just look at the results.

Check it out

OK, now our self-made image is ready. You can run it locally and experiment with it.

3. Upload the image to Alibaba Cloud Warehouse

(1) Create your own Docker repository on Alibaba Cloud.

(2) Upload the image

1. Log in to Alibaba Cloud Docker Registry and remember to change your username to your own.

docker login --username=your Alibaba Cloud username registry.cn-hangzhou.aliyuncs.com

The login password is your Alibaba Cloud login password

2. Enter the following two lines of commands to push the image to the Registry

docker tag [image ID] registry.cn-hangzhou.aliyuncs.com/namespace/warehouse 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 finish running.

I uploaded it here before.

4. Pull this image on the server and run it (of course, Docker must be installed on the server first)

docker pull registry.cn-hangzhou.aliyuncs.com/namespace/warehouse name:[image version number]

Log in to the server via ssh, enter the command above, and then check if it is pulled down.

4. Run it and see if it works

Here I map docker's port 8080 to the server's port 28080

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

This is the end of this article about the implementation steps of Docker deployment of Flask application. For more relevant content about Docker deployment of Flask application, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Analysis of table names and parent class related issues of custom model classes in Flask and Django frameworks
  • How to deploy flask application to server
  • Example of how to deploy a Flask project with uWSGI and Nginx
  • Deploy Nginx+Flask+Mongo application using Docker
  • CentOS7 deployment Flask (Apache, mod_wsgi, Python36, venv)
  • Python project migration and deployment based on Flask framework configuration dependency package information
  • How to deploy the flask project on CentOS
  • CentOS 7.0 uses Nginx to deploy flask application tutorial
  • Alibaba Cloud Deployment of Ubuntu 1.4 Flask + WSGI + Nginx Detailed Explanation
  • Deploy the flaskblog application on a DigitalOcean server
  • Tutorial on deploying Nginx, FastCGI and Flask framework on Mac OS
  • Tutorial on deploying Python's Flask framework on Docker
  • How to deploy a model as a service using flask

<<:  Vue scroll down to load more data scroll case detailed explanation

>>:  Mysql string interception and obtaining data in the specified string

Recommend

Simple example of adding and removing HTML nodes

Simple example of adding and removing HTML nodes ...

PHP scheduled backup MySQL and mysqldump syntax parameters detailed

First, let's introduce several common operati...

React's component collaborative use implementation

Table of contents Nesting Parent-child component ...

Unicode signature BOM detailed description

Unicode Signature BOM - What is the BOM? BOM is th...

Detailed explanation of how to pass password to ssh/scp command in bash script

Install SSHPASS For most recent operating systems...

Detailed code for implementing 3D tag cloud in Vue

Preview: Code: Page Sections: <template> &l...

How to clear floating example code in css

Overview The framework diagram of this article is...

Detailed explanation of MySQL date addition and subtraction functions

1. addtime() Add the specified number of seconds ...

MySQL 5.7.17 installation graphic tutorial (windows)

I recently started learning database, and I feel ...

Vue implements drag and drop or click to upload pictures

This article shares the specific code of Vue to a...

Vue implements time countdown function

This article example shares the specific code of ...

The difference between char, varchar and text field types in MySQL

In MySQL, fields of char, varchar, and text types...

Centos7 startup process and Nginx startup configuration in Systemd

Centos7 startup process: 1.post(Power-On-Self-Tes...

Specific use of MySQL segmentation function substring()

There are four main MySQL string interception fun...