Docker installation and Flask project deployment

Posted by ouch! on Sat, 15 Jan 2022 16:17:59 +0100

Installing docker for linux

Install online using yum or up2date

Installing docker in CentOS 7 requires a 64 bit system and a system kernel version of 3.10 or above. You can use the following command to view it

uname -r
1. Check whether docker list has been installed
yum list installed | grep docker
2. Install docker
yum -y install docker

-y means do not ask for installation until the installation is successful. After the installation, check the installation list again

3. Start docker
systemctl start docker
4. Set to startup
systemctl enable docker.service
5. View docker service status
systemctl status docker

The figure above shows that docker has been started successfully

Offline installation

1. Download the installation package

Installation package official address: , you can download it locally and then upload it to the server through ftp tool, or use the command to download it on the server

2. Decompression
tar -zxvf docker-18.06.3-ce.tgz
3. Copy the extracted docker file to the / usr/bin / directory
cp docker/* /usr/bin/
4. Add under / etc/systemd/system / The content of the service file is as follows. In this way, the docker can be registered as a service
Description=Docker Application Container Engine
Documentation= firewalld.service
# the default is not to use systemd for cgroups because the delegate issues still
# exists and systemd currently does not support the cgroup feature set required
# for containers run by docker
ExecStart=/usr/bin/dockerd --selinux-enabled=false --insecure-registry=
ExecReload=/bin/kill -s HUP $MAINPID
# Having non-zero Limit*s causes performance problems due to accounting overhead
# in the kernel. We recommend using cgroups to do container-local accounting.
# Uncomment TasksMax if your systemd version supports it.
# Only systemd 226 and above support this version.
# set delegate yes so that systemd does not reset the cgroups of docker containers
# kill only the docker process, not all processes in the cgroup
# restart the docker process if it exits prematurely

The – secure registry = (changed to your private server ip here) setting here is to allow docker s to access unsafe when their own private server Harbor is built, otherwise access will be denied.

5. Start

To docker Add execution permission to service file

chmod +x /etc/systemd/system/docker.service 

Reload the configuration file (reload it every time you modify the docker.service file)

systemctl daemon-reload    


systemctl start docker

Set startup

systemctl enable docker.service

View docker service status

systemctl status docker

Encapsulating a flash project with docker


1. Export the project's third-party dependency package to requirements txt
pip freeze >requirements.txt
2. Create gunicorn py

Gunicorn is a high-performance Python WSGI UNIX HTTP Server widely used on unix.
It is compatible with most web frameworks, and has the characteristics of simple implementation, lightweight and high performance.

# Listening port
bind = ''
# Working mode
worker_class = 'gevent'  # Gevent library is adopted to support asynchronous processing of requests and improve throughput. In addition to gevent, there are eventlet s

# Number of parallel work processes (I use the same number as the CPU core)
# workers = multiprocessing.cpu_count() * 1
workers = 5 # Define the number of processes that are started at the same time to process requests, and adjust them appropriately according to the website traffic

# Set daemon
daemon = False

# Displays the current configuration. The default value is False.
check_config = False

# Set logging level
loglevel = 'debug'

# Set error message log path
errorlog = '/home/slp/Project/StockQTS/logs/error.log'  #/root/project/BlueWhale/logs/error.log    /home/slp/Project/BlueWhale/logs/error.log
# Set access log path
accesslog = '/home/slp/Project/StockQTS/logs/access.log' #/root/project/BlueWhale/logs/access.log   /home/slp/Project/BlueWhale/logs/access.log

You can use the gunicorn command to test whether it can run correctly. The command is as follows. Open the website and our website will be opened.

gunicorn manage:app -c
3. Create Dockerfile file
FROM python:3.7
WORKDIR /home/slp/Project/StockQTS

COPY requirements.txt ./
RUN pip install -r requirements.txt -i

COPY . .

CMD ["gunicorn", "manage:app", "-c", "./"]

Build Docker image

1. Enter the project folder
cd /home/slp/Project/StockQTS
2. To build an image, enter the following command
sudo docker build -t 'stack_flask' .

Then it will perform six steps. If there are many dependencies, it will take more time to install dependencies.

3. View the image
sudo docker images

The self-made image is ready. You can run locally and experiment

Upload the image to Alibaba cloud warehouse

1. Create your own Docker warehouse on alicloud.

2. Upload image

Before uploading the image, you need to set the login password in warehouse management - > access credentials.

Sign in

sudo docker login --username=Your alicloud user name
Login Succeeded

Enter the password. The password is the password you just set for your access credentials
Enter the following two lines of commands to push the image to Registry

docker tag [mirrored  ID] name:[Mirror version number]
docker push name:[Mirror version number]

It's OK after execution.

3. Pull the image
sudo docker pull name:[Mirror version number]

Check whether it is pulled down

sudo docker images
4. Operation
sudo docker run -itd -p 8000:8000 --name stock_flask 

So here, the Flask project has been successfully deployed. When updating the project, maintain the configuration file, build it, push it, pull it down on the server and run it again.

Topics: Flask