詳解如何使用Docker部署Django+MySQL8開(kāi)發(fā)環(huán)境
前一段時(shí)間重裝了系統(tǒng),然后我還沒(méi)有備份,導(dǎo)致電腦里的開(kāi)發(fā)環(huán)境全都沒(méi)有了。
一想到又要裝 Python 環(huán)境,還要裝數(shù)據(jù)庫(kù),然后安裝過(guò)程中還可能報(bào)一堆錯(cuò)就頭疼。
最近正在學(xué)習(xí) Docker,這不正好解決了我當(dāng)前的痛點(diǎn)了嗎?而且,不止這次重裝系統(tǒng),以后再重裝都不怕了,只要拿著 Dockerfile 和 docker-compose 文件,不管到什么環(huán)境,一條命令輕松跑起來(lái)。
之前部署 Python 開(kāi)發(fā)環(huán)境,都是用的 virtualenv,或者是 Pipenv。這次使用 Docker 之后,對(duì)比下來(lái),還是 Docker 更加方便,下面就來(lái)詳細(xì)介紹。
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/
使用 Dockerfile 來(lái)創(chuàng)建鏡像,Python 版本是 3.6.8,將源代碼拷貝到容器中 /code 目錄。
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
使用 docker-compose 來(lái)編排容器,一共啟兩個(gè)服務(wù),web 服務(wù)就是后臺(tái)的 Django 服務(wù),mysql 是數(shù)據(jù)庫(kù)服務(wù)。
有三點(diǎn)需要注意:
- web 服務(wù)使用 depends_on 命令,表示依賴(lài)于 mysql 服務(wù)。
- mysql 服務(wù)一定要加 --default-authentication-plugin=mysql_native_password 命令。因?yàn)閺?MySQL 8.0 開(kāi)始,默認(rèn)的加密規(guī)則使用的是 caching_sha2_password,而我們的客戶(hù)端并不支持。之前使用的是 mysql_native_password。
- 使用 volumes 來(lái)持久化數(shù)據(jù),否則容器刪除之后,數(shù)據(jù)就都丟了。
requirements
Django==2.2.11 mysqlclient==1.4.6
啟動(dòng) Django 需要的 pip 包,Django 版本至少要 2.0,否則會(huì)報(bào)錯(cuò)。
Django settings
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'dev',
'USER': 'dev',
'PASSWORD': '123456',
'HOST': 'mysql',
'PORT': '3306'
}
}
在 Django settings 文件中配置數(shù)據(jù)庫(kù)信息,內(nèi)容需要與 docker-compose 中一致。
有一點(diǎn)需要注意,HOST 一定要配置成 docker-compose 中的服務(wù)名稱(chēng),在我這里是 mysql。配置成其他,比如 localhost 或者 127.0.0.1 會(huì)報(bào)錯(cuò)。
因?yàn)?Docker 啟動(dòng)時(shí)會(huì)設(shè)置一個(gè)本地網(wǎng)絡(luò),可以將 mysql 解析到對(duì)應(yīng)服務(wù)的容器,而對(duì)應(yīng)的服務(wù)并不在 localhost 上。
Run
使用如下命令創(chuàng)建鏡像。
$ docker-compose -f ./docker-compose.yml build
也可以省略上一步,直接使用如下命令啟動(dòng)服務(wù),如果沒(méi)有鏡像,會(huì)先創(chuàng)建鏡像,然后再啟動(dòng)服務(wù)。
$ docker-compose -f ./docker-compose.yml up
排錯(cuò)
在部署過(guò)程中,可能會(huì)碰到如下這些錯(cuò)誤,基本都是配置錯(cuò)誤造成的。如果發(fā)生了,一定要仔細(xì)檢查配置,只要和文中相同,是不會(huì)有問(wèn)題的。
- '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)")
我還遇到一個(gè)比較坑的問(wèn)題是這個(gè):
[Warning] root@localhost is created with an empty password ! Please consider switching off the --initialize-insecure option.
我以為是我的密碼設(shè)置不正確,檢查了好久都沒(méi)發(fā)現(xiàn)問(wèn)題,后來(lái)在網(wǎng)上找到了解釋?zhuān)苯雍雎跃托辛恕?/p>
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.
參考文檔:
http://fusionblender.net/django-and-mysql-8-using-docker/
https://github.com/docker-library/mysql/issues/307…
https://www.jianshu.com/p/4eafa4f87fd5
到此這篇關(guān)于詳解如何使用Docker部署Django+MySQL8開(kāi)發(fā)環(huán)境的文章就介紹到這了,更多相關(guān)Docker部署Django+MySQL8內(nèi)容請(qǐng)搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信