程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Five minutes, Ill teach you how to deploy a python application with docker!

編輯:Python

There are some pain points when deploying projects on traditional physical machines or cloud servers

such as : Project deployment is slow 、 Waste of resources 、 Difficult migration 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

1. Dockerfile Description file

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

    The files of the host will be , And unpack it

  • COPY

    and ADD The command function is the same , But it will not decompress

  • WORKDIR

    Used to switch working directory

  • VOLUME

    Configure the directory mapping between the host and the container

  • EXPOSE

    Configure the exposed port number of items in the container

  • CMD

    After the specified container starts , Running commands

    such as , You can run a command to start the project

2. Let's fight

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/

3. summary

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 startup program and parameters , This part of the content can be expanded according to the introduction of the official website

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 !

Recommended reading

Automation | Realize the super detailed process of automatically grabbing Maotai !

5 minute , Teach you to quickly write an oil monkey script from zero !

How to use Python Realize the freedom of lottery ( Big lotto )

How to use Python Realize the freedom of lottery ( Bicolor )

END

Haowen and his friends watch ~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved