Docker practice: Python application containerization

Docker practice: Python application containerization

1. Introduction

Containers use a sandbox mechanism to isolate each other. The advantage is that the applications deployed in the container do not affect each other and run independently, providing higher security. This article mainly introduces running Python applications (Django) in Docker containers, writing Dockerfiles to automate image building, and the Docker artifact Compose.

2. Write the Dockerfile file

The Python image downloaded from the official website is relatively streamlined, and web application-related dependencies still need to be installed by yourself. Writing a Dockerfile allows you to automate the process of building an image. The following are examples:

FROM python:3.6.4

RUN mkdir /code \
&&apt-get update \
&&apt-get -y install freetds-dev \
&&apt-get -y install unixodbc-dev
COPY app /code 
COPY requirements.txt /code
RUN pip install -r /code/requirements.txt -i https://pypi.douban.com/simple
WORKDIR /code

CMD ["/bin/bash","run.sh"]

FROM: A very important command in Dockerfile, which is used to specify a base image for the build process. For example, python3.6.4 is specified as the base image above, and all subsequent operations will be customized based on this image. If it does not exist, it will be downloaded from the official website. FROM must be the first command in a Dockerfile.

RUN: The core part of the Dockerfile execution command, which executes the parameters during the image building process.

COPY: Copy files. COPY <source path> <destination path>

WORKDIR: Working directory. If it does not exist, it will be created for you automatically.

CMD: container startup command. Docker is not a virtual machine, but a container is a process. Since it is a process, when starting the container, you need to specify the program and parameters to be run. The CMD instruction is used to specify the startup command of the default container main process. If docker run specifies a command parameter, the cmd here will not work. For example, docker run -it -name redis docker.io/redis /bin/bash, starting the container will not execute the cmd in the Dockerfile, because docker run has already specified the command parameter /bin/bash.

3. Build an Image

docker build [OPTIONS] context path | URL

  • [OPTIONS]: Usually the command includes -t, which is used to specify the name of the image. -f specifies the context path of the Dockfile.
  • Context path|URL: Context path. If there is only one dot ".", it represents the current directory.

The docker bulid -t webtest . command builds an image named webtest and returns an image id 1dfa2905efac after the build is complete.

[root@CentOS webtest]# ls
app Dockerfile requirements.txt run.sh
[root@CentOS webtest]# docker build -t webtest . 
...
...
...
Removing intermediate container 9c510e88e659
Step 6/6 : CMD /bin/bash run.sh
---> Running in 0bd29255c648
---> 1dfa2905efac
Removing intermediate container 0bd29255c648
Successfully built 1dfa2905efac
  • app: Django project
  • Dockerfile
  • requirements.txt is the python library required to run the project
    • Django
    • Django 2
    • pyDes
    • PyMySQL
    • redis
    • requests
    • pymssql
    • pyodbc
    • paramiko
    • psutil

run.sh is the shell script that needs to be called when running the container

python /code/app/manage.py runserver 0.0.0.0:8000

Start the container and run the image just built.

docker run -it -p 6500:8000 -v /home/code/webtest:/code --name web --restart always --privileged=true webtest

[root@CentOS webtest]# docker run -it -p 6500:8000 -v /home/code/webtest:/code --name web --restart always --privileged=true webtest
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

August 09, 2018 - 09:56:51
Django version 2.1, using settings 'ShiHangTool.settings'
Starting development server at http://0.0.0.0:8000/
Quit the server with CONTROL-C.

-p: Map the container's port 8000 to the host's port 6500

-v: The host directory /home/code/webtest is mapped to the container directory /code

--name: Name the container web, webtest is the image we just built

--restart: always The container is always restarted when it exits

--privileged=true: Permissions required to execute files in the container

Enter ip:6500/Home/OrderSettle-K8S/

Run successfully!

5. Compose

Do you think the above operations are too complicated? It would be great if they could be deployed automatically. Don't worry, compose can help you

1. Introduction:

Compose is an official open source project of Docker, which is used for rapid orchestration of Docker clusters. Compose defines and runs one or more containers through a docker-compose.yml file. It is an upgraded version of fig.

2. Installation:

Compose is written in Python and calls the API provided by Docker to manage the container. so can be installed through the python management tool pip

pip install docker-compose

3. Write the docker-compose.yml file

This is the docker-compose.yml main template format

version: '3'
services:
  web1:
    build: .
    image: web1
    ports:
      - "7500:8000"
    volumes: 
      - /home/code/webtest:/code
    privileged: true
    restart: always

4. Run the compose project

Put dockerfile, requirements.txt, docker-compose.yml, and app folder in the same directory and run the command docker-compose up

[root@CentOS webtest]# docker-compose up
Creating network "webtest_default" with the default driver
Building web1
Step 1/6: FROM python3.6.4-dev
 --->ca46b1ed99ab
Step 2/6: COPY app /code
 ---> f59b9540f8ab
Removing intermediate container e987c66b51f5
Step 3/6 : COPY requirements.txt /code
 ---> 2095b64882ac
Removing intermediate container e3099b386727
...
...
...

After the run is complete, docker ps checks the container web1 that has just been started and it is already running.

[root@CentOS ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eeab6b5a993b web1 "/bin/bash run.sh" About a minute ago Up 59 seconds 0.0.0.0:7500->8000/tcp webtest_web1_1
5fb51ce5a51c webtest "/bin/bash run.sh" 23 hours ago Up About an hour 0.0.0.0:6500->8000/tcp web

5. Summary

Compose configures the two steps of building an image and running a container in a yml file to achieve automated deployment.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Django Docker container deployment Django-Docker local deployment
  • Summary of Node.js service Docker container application practice
  • .NETCore Docker implements containerization and private image repository management
  • Docker large-scale project containerization transformation
  • Docker container deployment attempt - multi-container communication (node+mongoDB+nginx)
  • Detailed explanation of Docker containerized spring boot application
  • Detailed explanation of using ELK to build a Docker containerized application log center
  • Analysis of the process of deploying Python applications in Docker containers

<<:  How to use Nginx to handle cross-domain Vue development environment

>>:  Solution to the lack of my.ini file in MySQL 5.7

Recommend

Use of Linux gzip command

1. Command Introduction The gzip (GNU zip) comman...

Ubuntu 20.04 turns on hidden recording noise reduction function (recommended)

Recently, when using kazam in Ubuntu 20.04 for re...

JS removeAttribute() method to delete an attribute of an element

In JavaScript, use the removeAttribute() method o...

Ideas and codes for implementing Vuex data persistence

What is vuex vuex: is a state manager developed s...

User-centered design

I've been asked a lot lately about an apparen...

How to display texture at the position of swipe in CocosCreator

Table of contents 1. Project requirements 2. Docu...

Detailed steps to install JDK and Tomcat in Linux environment

Table of contents 1. Install JDK Manual Installat...

How to build YUM in Centos7 environment

1. Enter the configuration file of the yum source...

Detailed steps for creating a Vue scaffolding project

vue scaffolding -> vue.cli Quickly create a la...

The table merges cells and the img image to fill the entire td HTML

Source code (some classes deleted): Copy code The ...

Detailed explanation of using top command to analyze Linux system performance

Introduction to Linux top command The top command...

Should I use UTF-8 or GB2312 encoding when building a website?

Often when we open foreign websites, garbled char...