This is the 20th day of my participation in the More text Challenge. For more details, see more text Challenge

Docker is an open source project that provides an open platform for developers and system administrators to build, package, and run applications anywhere in a lightweight container. Docker automatically deploies applications in the software container.

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

The environment

The 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_64Copy the code

The Docker version

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

The Docker Compose version

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

The directory structure

This section lists the related files and directories. The functions and contents of each file directory are described as follows.

.├ ── docker-agem.yml ├─.env ├─ services │ ├─ Python │ ├── Dockerfile │ ├─ requirements. TXT └ ─ ─ WWW └ ─ ─ pythonCopy the code
  • Bash. Alias: command used to record local terminals.
  • Docker-compose. Yml: Container configuration file.
  • Env: environment variable setting file.
  • Services/python/Dockerfile: mirror build files.
  • Services/python/requirements. TXT: rely on package management.
  • WWW/Python: directory for projects/code.

Build the deployment

Setting environment variables

Open the.env file and add the following:

PYTHON_VERSION=3.8.2 PYTHON_PORT=9100Copy the code
  • PYTHON_VERSION: This is used to set Python Tags, all of which can be viewed on Docker Hub.

  • PYTHON_PORT: the port of the locally mapped container port.

Build the mirror

Open the services/python/Dockerfile file, add the following content:

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/simpleCopy the code

Let me describe each part:

  1. First, specify the Python image on which to build the image. This is the official image provided by the Docker organization, the Python image version by environment variablesPYTHON_VERSIONSettings. We give the image a namepython-base, which will be used in the next phase:
ARG PYTHON_VERSION
FROM python:${PYTHON_VERSION} AS python-base 
Copy the code
  1. Next, set the environment variables to set the locale correctly, preventing Python generation.pycFile, and enable Python tracing on segfaults:
ENV LANG C.UTF-8
ENV LC_ALL C.UTF-8
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONFAULTHANDLER 1 
Copy the code
  1. Finally, usepython-baseThe image begins a new construction phase. We will be inpython-depsInstall all Python dependencies in the image:
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 
Copy the code

Dependency Package Management

Open the services/python/requirements. TXT file, add the project need to rely on. Example:

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.0Copy the code

The container configuration

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

version: "3" services: python: build: context: ./services/python args: PYTHON_VERSION: ${PYTHON_VERSION} command: Python3 / code/HelloWorld/manage. Py runserver then executes 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:Copy the code

For more information about Docker Compose, please refer to the official documentation.

This is the configuration of the container after the execution of the command, similar to the Dockerfile CMD.

There are two more commands in the configuration, one of which is commented, and only one of them can be used. The first is the configuration of a single command, and the following is the configuration of multiple commands. Modify the commands and paths as you see fit (for an initial test, use the following command, which automatically initializes a HelloWorld project and starts).

Start the container

Run the following command at the root of the file:

docker-compose up -d 
Copy the code

Running this command automatically builds an image and starts the container. After the command is executed:

Viewing a mirror:

docker images 
Copy the code

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 the container:Copy the code

docker-compose ps -a

Name Command State Ports ------------------------------------------------------------------------------------------------- python python3 /code/HelloWorld/m ... Up 0.0.0.0:9100->8000/ TCP ``` `State If the State is Up, the IP address is successfully started. Open a browser and try it out:! [] (https://www.cnblogs.com/###qg1bzp-1-png###) run successfully. Here are some common commands: ** Start the container ** : 'docker-compose start python' ** Stop the container ** : 'docker-compose stop Python' ** Restart the container ** : Docker-compose rm python 'docker-compose && Docker-compose rm python' ** 'docker logs Python' Host Using the Python command ================= Terminal commands are recorded in the file 'bash.alias'. Here's how to use the 'python' command locally: Open '~/.bashrc' (if the ZSH client uses' ~/.zshrc ') and add the following code to the file:  ``` python () { tty= tty -s && tty=--tty docker run \ $tty \ --interactive \ --rm \ --volume $PWD:/code:rw \ --workdir /code \ dnmp_python python "$@"} ' ' Bashrc '(if ZSH client uses' source ~/.zshrc') dbnuo@localhost ~ python -v python 3.8.2Copy the code