Explain how to deploy the Django+MySQL8 development environment using Docker
- 2021-08-21 21:48:59
- OfStack
I reinstalled the system a while ago, and then I didn't back it up, which led to the loss of all the development environment in the computer.
1 thought to install Python environment, but also install database, and then the installation process may report a pile of errors on the headache.
I am learning Docker recently, doesn't this just solve my current pain point? Moreover, not only reinstall the system this time, but also reinstall it in the future. As long as you hold Dockerfile and docker-compose files, no matter what environment you go to, you can easily run with one command.
Previously, Python development environment was deployed with virtualenv or Pipenv. After using Docker this time, Docker is more convenient by comparison, which will be described 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/
Create the image using Dockerfile, version 3.6. 8 of Python, 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 arrange the container, 1 open two services altogether, web service is the background Django service, mysql is the database service.
There are three points to note:
The web service uses the depends_on command, indicating that it depends on the mysql service. mysql Service 1 must add the command default-authentication-plugin=mysql_native_password. Because starting with 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 the data, otherwise the data will be lost after the container is deleted.
requirements
Django==2.2.11
mysqlclient==1.4.6
pip package required to start Django, Django version should 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 the database information in the Django settings file, the content needs to be the same as in docker-compose 1.
One thing to note is that HOST 1 must be configured as the service name in docker-compose, which in my case is mysql. Configuration to something else, such as localhost or 127.0. 0.1, will report an error.
Because Docker starts with a local network, mysql can be resolved to the container of the corresponding service, which is not on localhost.
Run
Create the image using the following command.
$ docker-compose -f ./docker-compose.yml build
You can also skip the previous step and start the service directly using the following command. If there is no mirror, the mirror 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, 1 be sure to check the configuration carefully, as long as it is the same as in the text, 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 comparison pit problem, which is this:
[Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
I thought my password was incorrect. I checked it for a long time and didn't find any problems. Later, I found an explanation on the Internet, so I just ignored 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 documentation:
http://fusionblender.net/django-and-mysql-8-using-docker/
https://github.com/docker-library/mysql/issues/307 …
https://www.jianshu.com/p/4eafa4f87fd5