There are some pain points when deploying projects on traditional physical machines or cloud servers
such as : project Slow deployment 、 Waste of resources 、 transfer Difficult and low expansion
While using Docker The advantages of deploying a project include :
Efficient use of system resources
Faster service startup
The environment is the same , Easier migration
This article will introduce Docker Deploy a Python The general process of the project
Dockerfile Is a description file placed in the root directory of the project , You can use Docker The command builds an image based on this file
Common instructions include :
FROM
Used to define the underlying image
MAINTAINER
Specify maintainer information , You can omit it
RUN
and 「 Installation command 」 come together , Can be used to install tool dependency packages
ADD
take Host files , And unpack it
COPY
and ADD The command function is the same , however Do not decompress
WORKDIR
For switching working directory
VOLUME
Configure the host and container Directory mapping
EXPOSE
Configure in container items foreign Exposed port number
CMD
After the specified container starts , Running commands
Than Such as , Sure function A command starts the project
Use Docker The normal process of deploying an application is :
Develop the project and pass the local test
To write Dockerfile Put it in the project root directory
Package image file
Run the image container
test
For demonstration purposes , Here's a simple Flask The project is explained as an example
2-1 Project development
from flask import Flask
# Installation dependency
# pip3 install -U flask
app = Flask(__name__)
@app.route('/')
def index():
return " Test container deployment !"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8888)
# Browser access test
# http://127.0.0.1:8888/
Project development completed , After passing the local test, you can write Dockerfile The file
2-2 To write Dockerfile
Under the project root , Create a Dockerfile file , Use the above instructions to write a description script
It should be noted that , Use here 「 EXPOSE 」 The port number exposed by the instruction is consistent with the port number defined in the entry file
# Dockerfile
FROM centos:7.9.2009
RUN yum makecache fast;
RUN yum install python3-devel python3-pip -y
RUN pip3 install -i https://pypi.douban.com/simple flask
COPY main.py /opt
WORKDIR /opt
EXPOSE 8888
CMD ["python3","main.py"]
2-3 Build a mirror image
# Under the current folder , according to Dockerfile File to build an image
# Image name :xag/my_flask_web
# --no-cache: Do not use the old cache for image building
docker build --no-cache -t "xag/my_flask_web" .
2-4 Run the image container
Use docker run The command runs a container based on the image
among
-d: Represents that the container is running in the background , Not based on the foreground
--name: Alias used to execute the container
-p: Used to configure port mapping between host and container
# -d: Background operation
# The host machine (9999) Map the... In the container 8888( above Dockerfile It's been exposed 8888 port )
docker run -d --name flask_web -p 9999:8888 xag/my_flask_web
2-5 Test it
Finally in the browser , The port number exposed by the host 9999 Visit the project
Access address :http://127.0.0.1:9999/
In the article, a simple Web The project describes the use of Docker The general process of deploying a project
actually ,Dockerfile Very flexible , It also supports ARG/ENV Set the environment variable ,VOlUME Instruction mount Directory ,ENTRYPOINT Configure the launcher and parameters etc. , This part of the content can be based on The official website introduces self expansion
https://docs.docker.com/engine/reference/builder/
If you think the article is good , Please give the thumbs-up 、 Share 、 Leaving a message. Next , Because this will be the strongest driving force for me to continue to output more quality articles !
END