Detailed explanation of how to use Docker to deploy Django+MySQL8 development environment

Detailed explanation of how to use Docker to deploy Django+MySQL8 development environment

I reinstalled the system some time ago, but I didn't back it up, so all the development environments on my computer were gone.

It’s a headache to think about installing the Python environment and the database, and then there may be a lot of errors during the installation process.

I'm currently learning Docker. Doesn't this just solve my current pain points? Moreover, you don’t have to worry about reinstalling the system not only this time, but also in the future. As long as you have the Dockerfile and docker-compose files, you can run it easily with one command no matter what environment you are in.

Previously, Python development environments were deployed using virtualenv or Pipenv. After using Docker this time, I found that Docker is more convenient. Let me introduce it in detail below.

Dockerfile

FROM python:3.6.8

ENV PYTHONUNBUFFERED 1

RUN mkdir -p /code
COPY ./requirements.txt /code

WORKDIR /code

RUN sed -i "s/archive.ubuntu./mirrors.aliyun./g" /etc/apt/sources.list
RUN sed -i "s/deb.debian.org/mirrors.aliyun.com/g" /etc/apt/sources.list

RUN apt-get clean && apt-get -y update && \
 apt-get -y install libsasl2-dev python-dev libldap2-dev libssl-dev libsnmp-dev
RUN pip3 install --index-url https://mirrors.aliyun.com/pypi/simple/ --no-cache-dir -r requirements.txt

COPY ./* /code/

Use Dockerfile to create the image, the Python version is 3.6.8, and copy the source code to the /code directory in the container.

docker-compose

version: '3'

services:
 web:
 build:
  context: .
  dockerfile: Dockerfile
 image: web
 container_name: web
 hostname: web
 restart: always
 command: python /code/manage.py runserver 0.0.0.0:8000
 volumes:
  - .:/web
 ports:
  - "8000:8000"
 depends_on:
  -mysql 

 mysql:
 image: mysql
 container_name: mysql
 hostname:mysql
 restart: always
 command: --default-authentication-plugin=mysql_native_password --mysqlx=0
 ports:
  -3306:3306
 volumes:
  - ./db:/var/lib/mysql
 environment:
  -MYSQL_HOST=localhost 
  -MYSQL_PORT=3306 
  -MYSQL_DATABASE=dev
  -MYSQL_USER=dev
  -MYSQL_PASSWORD=123456
  -MYSQL_ROOT_PASSWORD=123456

Use docker-compose to orchestrate the containers and start two services in total. The web service is the background Django service, and mysql is the database service.

There are three points to note:

  • The web service uses the depends_on command to indicate that it depends on the mysql service.
  • The mysql service must add the --default-authentication-plugin=mysql_native_password command. Because starting from MySQL 8.0, the default encryption rule uses caching_sha2_password, which our client does not support. Previously, mysql_native_password was used.
  • Use volumes to persist data, otherwise the data will be lost after the container is deleted.

requirements

Django==2.2.11
mysqlclient==1.4.6

Start the pip package required by Django. The Django version must be at least 2.0, otherwise an error will be reported.

Django settings
DATABASES = {
 'default': {
  'ENGINE': 'django.db.backends.mysql',
  'NAME': 'dev',
  'USER': 'dev',
  'PASSWORD': '123456',
  'HOST': 'mysql',
  'PORT': '3306'
 }
}

Configure database information in the Django settings file. The content must be consistent with that in docker-compose.

One thing to note is that HOST must be configured as the service name in docker-compose, in my case it is mysql. If you configure it to other values, such as localhost or 127.0.0.1, an error will be reported.

Because Docker sets up a local network when it starts, it can resolve mysql to the container of the corresponding service, and the corresponding service is not on localhost.

Run

Use the following command to create a mirror.

$ docker-compose -f ./docker-compose.yml build

You can also skip the previous step and directly start the service using the following command. If there is no image, an image will be created first and then the service will be started.

$ docker-compose -f ./docker-compose.yml up

Troubleshooting

During the deployment process, you may encounter the following errors, which are basically caused by configuration errors. If this happens, be sure to check the configuration carefully. As long as it is the same as in the article, there will be no problem.

  • 'Plugin caching_sha2_password could not be loaded: /usr/lib/x86_64-linux-gnu/mariadb19/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory'
  • django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module.
  • django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on 'db' (115)")
  • django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")
  • django.db.utils.OperationalError: (2002, "Can't connect to MySQL server on '127.0.0.1' (115)")
  • django.db.utils.OperationalError: (2002, "Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)")

I also encountered a more difficult problem:

[Warning] root@localhost is created with an empty password! Please consider switching off the --initialize-insecure option.

I thought my password was set incorrectly and I checked for a long time but couldn't find any problem. Later I found an explanation online and decided to just ignore it.

That is just a warning printed by during database file initialization (mysqld --initialize-insecure). The root user with password is created later while the database is listening only on the unix socket.

Reference Documents:

http://fusionblender.net/django-and-mysql-8-using-docker/
https://github.com/docker-library/mysql/issues/307…
https://www.jianshu.com/p/4eafa4f87fd5

This is the end of this article on how to use Docker to deploy Django+MySQL8 development environment. For more relevant content about Docker deployment of Django+MySQL8, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementation of docker-compose deployment project based on MySQL8
  • Example of how to deploy MySQL 8.0 using Docker
  • How to install MySQL 8.0 in Docker
  • Upgrade Docker version of MySQL 5.7 to MySQL 8.0.13, data migration
  • How to install MySQL8 in Docker
  • Docker deployment MySQL8 cluster (one master and two slaves) implementation steps

<<:  Example to explain the size of MySQL statistics table

>>:  JavaScript file loading and blocking issues: performance optimization case study

Recommend

Detailed installation process of MySQL 8.0 Windows zip package version

The installation process of MySQL 8.0 Windows zip...

A quick guide to Docker

Docker provides a way to automatically deploy sof...

HTML form submission method case study

To summarize the form submission method: 1. Use t...

Detailed tutorial on installing Python 3.6.6 from scratch on CentOS 7.5

ps: The environment is as the title Install possi...

Detailed steps to configure MySQL remote connection under Alibaba Cloud

Preface As we all know, by default, the MySQL ins...

Mysql database index interview questions (basic programmer skills)

Table of contents introduction Indexing principle...

MySQL 8.0.23 Major Updates (New Features)

Author: Guan Changlong is a DBA in the Delivery S...

Pessimistic locking and optimistic locking in MySQL

In relational databases, pessimistic locking and ...

A brief discussion on creating cluster in nodejs

Table of contents cluster Cluster Details Events ...

The easiest way to install MySQL 5.7.20 using yum in CentOS 7

The default database of CentOS7 is mariadb, but m...

Parsing Apache Avro Data in One Article

Abstract: This article will demonstrate how to se...

The ultimate solution for writing bash scripts with nodejs

Table of contents Preface zx library $`command` c...