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

Getting Started Tutorial on Animating SVG Path Strokes Using CSS3

Without relying on JavaScript, pure CSS is used t...

SQL Server database error 5123 solution

Because I have a database tutorial based on SQL S...

Example code for using HTML ul and li tags to display images

Copy the following code to the code area of ​​Drea...

A brief analysis of MySQL backup and recovery

Table of contents 1. Introduction 2. Simple defin...

Detailed explanation of Docker basic network configuration

External Access Randomly map ports Using the -P f...

An article to show you how to create and use Vue components

Table of contents 1. What is a component? 2. Crea...

Native JS to achieve digital table special effects

This article shares a digital clock effect implem...

A brief introduction to the general process of web front-end web development

I see many novice students doing front-end develop...

Problems encountered in the execution order of AND and OR in SQL statements

question I encountered a problem when writing dat...

Using shadowsocks to build a LAN transparent gateway

Table of contents Install and configure dnsmasq I...

Mysql join query principle knowledge points

Mysql join query 1. Basic concepts Connect each r...

Rendering Function & JSX Details

Table of contents 1. Basics 2. Nodes, trees, and ...

mysql5.7 remote access settings

Setting up remote access in mysql5.7 is not like ...

Vue+js realizes video fade-in and fade-out effect

Vue+js realizes the fade in and fade out of the v...