From the public account: AirPython

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

For example: slow project deployment, waste of resources, difficult migration and low expansion

The advantages of using Docker to deploy projects include:

  • Efficient use of system resources

  • Services start faster

  • Consistent environment, easier migration

This article will introduce the general process of Docker deploying a Python project

1. Dockerfile description file

Dockerfile is a description file placed in the root directory of the project, you can use Docker commands to build an image based on this file

Common commands include:

  • FROM

    Used to define the base image

  • MAINTAINER

    Specify the maintainer information, you can omit it

  • RUN

    Linked with the "  install command  " , it can be used to install tool dependencies

  • ADD

    Unzip the host's files

  • COPY

    Same as ADD command, but no decompression

  • WORKDIR

    Used to switch working directory

  • VOLUME

    Configure the directory mapping between the host and the container

  • EXPOSE

    Configure the port number exposed by the project in the container

  • CMD

    Specifies the command to run after the container starts

    For example, you can run a command to start the project

2. Let's fight

The general process for deploying an application with Docker is:

  • Develop the project and test it locally

  • Write Dockerfile and place it in the project root directory

  • package image file

  • Run the image container

  • test

For the convenience of demonstration, here is a simple Flask project as an example to explain

2-1 Project Development

from flask import Flask

# 安装依赖
# pip3 install -U flask

app = Flask(__name__)


@app.route('/')
def index():
    return "测试容器部署!"


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=8888)

# 浏览器访问测试
# http://127.0.0.1:8888/

The project development is completed, and the Dockerfile file can be written after the local test is passed

2-2 Write Dockerfile

In the project root directory, create a Dockerfile file and write the description script using the above instructions

It should be noted that the port number exposed by the "EXPOSE" command here is consistent with the port number defined by 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 the image

# 在当前文件夹下,根据Dockerfile文件构建一个镜像
# 镜像名称:xag/my_flask_web
# --no-cache:不使用旧的缓存进行镜像构建
docker build --no-cache -t "xag/my_flask_web" .

2-4 Run the image container

Use the docker run command to run a container based on the image

in

  • -d: Represents the container to run in the background, not based on the foreground

  • --name: the alias used to execute the container

  • -p: used to configure the port mapping between the host and the container

# -d:后台运行
# 宿主机(9999)映射容器内的8888(上面Dockerfile已经暴露了8888端口)
docker run -d --name flask_web -p 9999:8888 xag/my_flask_web  

2-5 Test it

Finally, in the browser, access the project through the port number 9999 exposed by the host

Access address: http://127.0.0.1:9999/

3. Summary

The article describes the general process of deploying a project using Docker with a simple web project

In fact, Dockerfile is very flexible. It also supports ARG/ENV to set environment variables, VOlUME instructions to mount directories, ENTRYPOINT to configure startup programs and parameters, etc. You can expand this part according to the introduction on the official website.

https://docs.docker.com/engine/reference/builder/

--- EOF ---


Recommended↓↓↓