Docker containerized deployment of Python applications

Docker is an open source project that provides developers and system administrators with an open platform to build and package applications as a lightweight container and run them anywhere. Docker automatically deploys applications in software containers.

In this article, I'll show you how to dockerize a Python Django application and then use a  docker-compose script to deploy the application as a container to the docker environment.

environment

operating system


 dbnuo@localhost  ~  sw_vers
ProductName:	Mac OS X
ProductVersion:	10.15.3
BuildVersion:	19D76

 dbnuo@localhost  ~  uname -v
Darwin Kernel Version 19.3.0: Thu Jan  9 20:58:23 PST 2020; root:xnu-6153.81.5~1/RELEASE_X86_64

Docker version

 dbnuo@localhost  ~  docker -v
Docker version 19.03.8, build afacb8b

Docker Compose version


 dbnuo@localhost  ~  docker-compose -v
docker-compose version 1.25.4, build 8d51620a

Directory Structure

The relevant files and directories used this time are listed here, and the function and content of each file directory will be introduced below.

highlighter- Dockerfile

.
├── bash.alias
├── docker-compose.yml
├── .env
├── services
│   └── python
│   │   ├── Dockerfile
│   │   └── requirements.txt
└── www
    └── python
  • bash.alias : Used to record the commands of the local terminal.
  • docker-compose.yml : container configuration file.
  • .env : Environment variable setting file.
  • services/python/Dockerfile : Image build file.
  • services/python/requirements.txt : Dependency package management file.
  • www/python : project/code storage directory.

build deployment

set environment variables

Open  .env the file and add the following:

env

#
# python
#
PYTHON_VERSION=3.8.2
PYTHON_PORT=9100
  • PYTHON_VERSION : Used to set Python Tags, you can   view all Tags on Docker Hub .

  • PYTHON_PORT : The port that maps the container port locally.

build image

Open  services/python/Dockerfile the file and add the following:

dockerfile

ARG PYTHON_VERSION
FROM python:${PYTHON_VERSION} AS python-base

ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONFAULTHANDLER 1

FROM python-base AS python-deps

WORKDIR /code

RUN apt-get update \
    && apt-get -y install freetds-dev \
    && apt-get -y install unixodbc-dev

COPY requirements.txt ./

RUN pip install -r requirements.txt -i https://pypi.douban.com/simple

I describe each part below:

  1. First, specify the Python image to build the image on. This is an official image provided by the Docker organization, and the Python image version  PYTHON_VERSION is set by an environment variable. We name this image  python-base, which will be used in the next stage:

dockerfile

ARG PYTHON_VERSION
FROM python:${PYTHON_VERSION} AS python-base
  1. Next, set environment variables to properly set the locale, prevent Python from making  .pyc files, and enable Python tracebacks on segfaults:

dockerfile

ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONFAULTHANDLER 1
  1. Finally,  python-base start a new build phase using the image. We will  python-deps install all Python dependencies in the image:

dockerfile

FROM python-base AS python-deps

WORKDIR /code

RUN apt-get update \
    && apt-get -y install freetds-dev \
    && apt-get -y install unixodbc-dev

COPY requirements.txt ./

RUN pip install -r requirements.txt -i https://pypi.douban.com/simple

Dependency package management

Open  services/python/requirements.txt the file and add the dependencies required by the project. Example:

highlighter-

Django==3.0.4
djangorestframework==3.11.0
pyDes==2.0.1
PyMySQL==0.9.3
redis==3.4.1
requests==2.23.0
pyodbc==4.0.30
paramiko==2.7.1
psutil==5.7.0

container configuration

Open  docker-compose.yml the file and configure the container:

yaml

version: "3"
services:

  python:
    build:
      context: ./services/python
      args:
        PYTHON_VERSION: ${PYTHON_VERSION}
    command: python3 /code/HelloWorld/manage.py runserver 0.0.0.0:8000
#    command:
#      - /bin/sh
#      - -c
#      - |
#        django-admin startproject HelloWorld
#        python3 /code/HelloWorld/manage.py runserver 0.0.0.0:8000
    container_name: python
    hostname: python
    volumes:
      - ${SOURCE_DIR}/python:/code:rw
    expose:
      - "8000"
    ports:
      - "${PYTHON_PORT}:8000"
    privileged: true
    restart: always
    networks:
      - default

networks:
  default:

 

The main explanation here  command is that this is a command that is configured to be executed after the container starts, similar to a Dockerfile  CMD.

There are two more in the configuration  command, one of which is commented, and only one of the two can be used. The first is the configuration method of a single command, and the following is the configuration method of multiple commands. Modify the command and path according to your personal situation (for the first test, it is recommended to use the following command, which will automatically initialize a HelloWorld project and start it).

Start the container

Run the command at the root of the file:

shell

docker-compose up -d

Running this command will automatically build the image and start the container. After execution:

Check out the mirror:

shell

docker images

highlighter-

REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
dnmp_python                          latest              7218552b8814        17 hours ago        1.02GB
python                               3.8.2               f88b2f81f83a        3 weeks ago         933MB

View container:

shell

docker-compose ps -a

highlighter- CSS

    Name                   Command               State                    Ports
-------------------------------------------------------------------------------------------------
python          python3 /code/HelloWorld/m ...   Up      0.0.0.0:9100->8000/tcp

State If the status is Up, the startup is successful.

Open a browser and try it out:

 Run successfully. Here are a few commonly used commands:

Start the container :docker-compose start python

Stop the container :docker-compose stop python

Restart the container :docker-compose restart python

Delete container :docker-compose && docker-compose rm python

View logs :docker logs python

Host uses Python commands

Terminal commands are recorded in files  bash.alias .

Here's how to use the command locally  python :

Open  ~/.bashrc (if using zsh client  ~/.zshrc)

Add the following code to the file:

shell

python () {
    tty=
    tty -s && tty=--tty
    docker run \
        $tty \
        --interactive \
        --rm \
        --volume $PWD:/code:rw \
        --workdir /code \
        dnmp_python python "$@"
}

Refresh the configuration file to make it effective: source ~/.bashrc(if it is used by zsh client  source ~/.zshrc)

At this time, the command can be used locally  python , try:

shell

 dbnuo@localhost  ~  python -V
Python 3.8.2

Guess you like

Origin blog.csdn.net/m0_72557783/article/details/128428445