python test development django-180 Docker compose deployment django+mysql environment


To deploy django project, you need to use mysql database and write a Dockerfile file to deploy django container. When deploying multiple containers, docker compose can be used for container orchestration.

Docker compose environment preparation

Install the docker compose command line tool using pip3 of Python 3

pip3 install docker-compose

Installed version: 1.29 two

# pip3 show docker-compose
Name: docker-compose
Version: 1.29.2
Summary: Multi-container orchestration for Docker
Author: Docker, Inc.
Author-email: None
License: Apache License 2.0
Location: /root/python36/lib/python3.6/site-packages

After installation, find the bin directory address where the docker compose directory is located

find /root -name docker-compose

Add soft link

ln -s /your/path/bin/docker-compose /usr/bin/docker-compose

Docker compose file writing

1. First write the Dockerfile file to start django

FROM python:3.6.8
RUN sed -i s@/ /etc/apt/sources.list
RUN apt-get update
RUN apt-get -y install vim
RUN apt-get -y install lrzsz
RUN pip install --upgrade pip --index-url
ADD . /code
RUN pip install -r requirements.txt --index-url

# Open port

CMD does not write to the dockerfile file before executing the command. Because the database has not been started, configuring CMD to start django service will report an error

2. Modify settings Database configuration in py file

        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'hrun',  # New database name
            'USER': 'root',  # Database login
            'PASSWORD': '123456',  # Database login password
            'HOST': 'db',  # Address of the server where the database is located (link address of docker compose file is associated with db)
            'PORT': 3306,  # The listening port is 3306 by default


Version specifies docker compose YML file writing format version 3.0 services is a service that needs to be started. There are multiple service key value pairs

version: '3.0'
    restart: always
    image: mysql:5.7
    container_name: yy_mysql
      - ./db:/var/lib/mysql
      MYSQL_ROOT_PASSWORD: "123456"
      MYSQL_DATABASE: "hrun"
      TZ: "Asia/Shanghai"
      - 3306:3306
    restart: always
    build: .
    container_name: yy_web
    command: python runserver
      - 8000:8000
      - db
      - db

Services configure parameters for starting services

  • Image: Specifies the image used by the service
  • container_name: defines the name of the startup container
  • expose: defines the port used by the container (generally used to identify the port used by the image, which is convenient for mapping with ports)
  • ports: defines the mapping between host port and container port. Host IP + host port can be used for access. Host port: container port
  • volumes: volume mount path, which defines the mapping between the directory / file of the host and the directory / file of the container. Host path: container path
  • depend_on: specify the loading order of services. For example, the database service needs to run in front of the background service
  • restart: always: configure restart. docker will start the service every time it starts
  • links: connect the specified container to the current connection. You can set an alias. It is obsolete. networks is recommended
  • Environment: start container environment variable
  • Command: starts the cmd command executed by the container

Docker compose run

The web service in docker compose is a local image. You can use build to build the image first (you can also build the local image without building first, or directly up)

docker-compose build

Construction process

[root@iZ2]# docker-compose build
Building web
Sending build context to Docker daemon  289.2MB
Step 1/17 : FROM python:3.6.8
 ---> 48c06762acf0
Step 2/17 : MAINTAINER yoyo <>
 ---> Using cache
 ---> 128b4ae83de3
Step 3/17 : RUN sed -i s@/ /etc/apt/sources.list
 ---> Using cache
 ---> f974c67bcf87
Step 4/17 : RUN apt-get update
 ---> Using cache
 ---> d3ac17c4760f
Step 5/17 : RUN apt-get -y install vim
 ---> Using cache
 ---> 6d5c2962aa1d
Step 6/17 : RUN apt-get -y install lrzsz
 ---> Using cache
 ---> 16c6a337d525
Step 7/17 : RUN pip install --upgrade pip --index-url
 ---> Using cache
 ---> 8e9efdca8ab0
Step 8/17 : WORKDIR /code
 ---> Using cache
 ---> 37d16b8cb963
Step 9/17 : ADD . /code

Start service

docker-compose up

Start process

Starting yy_mysql ... done
Starting yy_web   ... done
Attaching to yy_mysql, yy_web
yy_mysql | 2021-12-28 21:25:22+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.
yy_mysql | 2021-12-28 21:25:22+08:00 [Note] [Entrypoint]: Switching to dedicated user 'mysql'
yy_mysql | 2021-12-28 21:25:22+08:00 [Note] [Entrypoint]: Entrypoint script for MySQL Server 5.7.36-1debian10 started.

Add the - d parameter to suspend background startup

docker-compose up -d

Stop service

docker-compose stop

docker ps can see that YY has been started_ MySQL and yy_web two containers

[root@iZ2]# docker ps
CONTAINER ID   IMAGE                           COMMAND                  CREATED          STATUS          PORTS                                                                                        NAMES
10495551676c   h2_web   "python ru..."   12 minutes ago   Up 15 seconds>8000/tcp                                                                       yy_web
76ade7a55ab4   mysql:5.7                       "docker-entrypoint.s..."   12 minutes ago   Up 15 seconds>3306/tcp, 33060/tcp

It should be noted that after such deployment, the instruction to synchronize the database needs to enter yy_web container execution

python makemigrations
python migrate

Added by pngtest on Wed, 29 Dec 2021 22:54:48 +0200