无法为 Django 和 Mysql 启动 docker 构建
Posted
技术标签:
【中文标题】无法为 Django 和 Mysql 启动 docker 构建【英文标题】:Unable to fire a docker build for Django and Mysql 【发布时间】:2020-04-20 14:18:42 【问题描述】:我正在使用 Djnago 和 mysql 构建一个应用程序。我想使用 docker 来部署我的应用程序。我准备了requirement.txt
、docker-compose.yml
和Dockerfile
docker-compose.yml
version: "3"
services:
law-application:
restart: always
build:
context: .
ports:
- "8000:8000"
volumes:
- ./app:/app
command: >
sh -c "python manage.py runserver 0.0.0.0:8000"
depends_on:
- mysql_db
mysql_db:
image: mysql:latest
command: mysqld --default-authentication-plugin=mysql_native_password
volumes:
- "./mysql:/var/lib/mysql"
ports:
- "3306:3306"
restart: always
environment:
- MYSQL_ROOT_PASSWORD=root
- MYSQL_DATABASE=root
- MYSQL_USER=root
- MYSQL_PASSWORD=root
requirements.txt
django>=2.1.3,<2.2.0
djangorestframework==3.11.0
mysqlclient==1.4.6
Dockerfile
FROM python:3.7-alpine
MAINTAINER Intersources Inc.
ENV PYTHONUNBUFFERED 1
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
RUN apt-get update
RUN apt-get install python3-dev default-libmysqlclient-dev -y
RUN mkdir /app
WORKDIR /app
COPY ./app /app
RUN adduser -D jeet
USER jeet
settings.py
DATABASES =
'default':
'ENGINE': 'django.db.backends.mysql',
'NAME': 'my-app-db',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'mysql_db',
'PORT': 3307,
我一直在尝试运行命令docker build .
以从 docker 文件构建映像,但出现此错误。看起来 MySql 连接器存在一些问题。我已经尝试寻找解决方案,但找不到任何可以解决此问题的方法。如果我从docker-compose.yml
中删除 mysql_db 服务,我就能够构建映像。
Sending build context to Docker daemon 444.7MB
Step 1/12 : FROM python:3.7-alpine
---> 6c7f85a86cca
Step 2/12 : MAINTAINER Intersources Inc.
---> Using cache
---> 03b6fa5764d4
Step 3/12 : ENV PYTHONUNBUFFERED 1
---> Using cache
---> 22ecd91dcb55
Step 4/12 : COPY ./requirements.txt /requirements.txt
---> Using cache
---> e58c16108f20
Step 5/12 : RUN pip install -r /requirements.txt
---> Running in 8f3eb8240fce
Collecting django<2.2.0,>=2.1.3
Downloading https://files.pythonhosted.org/packages/ff/82/55a696532518aa47666b45480b579a221638ab29d60d33ce71fcbd3cef9a/Django-2.1.15-py3-none-any.whl (7.3MB)
Collecting djangorestframework==3.11.0
Downloading https://files.pythonhosted.org/packages/be/5b/9bbde4395a1074d528d6d9e0cc161d3b99bd9d0b2b558ca919ffaa2e0068/djangorestframework-3.11.0-py3-none-any.whl (911kB)
Collecting mysqlclient==1.4.6
Downloading https://files.pythonhosted.org/packages/d0/97/7326248ac8d5049968bf4ec708a5d3d4806e412a42e74160d7f266a3e03a/mysqlclient-1.4.6.tar.gz (85kB)
ERROR: Command errored out with exit status 1:
command: /usr/local/bin/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-e9kt1otq/mysqlclient/setup.py'"'"'; __file__='"'"'/tmp/pip-install-e9kt1otq/mysqlclient/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-e9kt1otq/mysqlclient/pip-egg-info
cwd: /tmp/pip-install-e9kt1otq/mysqlclient/
Complete output (12 lines):
/bin/sh: mysql_config: not found
/bin/sh: mariadb_config: not found
/bin/sh: mysql_config: not found
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/tmp/pip-install-e9kt1otq/mysqlclient/setup.py", line 16, in <module>
metadata, options = get_config()
File "/tmp/pip-install-e9kt1otq/mysqlclient/setup_posix.py", line 61, in get_config
libs = mysql_config("libs")
File "/tmp/pip-install-e9kt1otq/mysqlclient/setup_posix.py", line 29, in mysql_config
raise EnvironmentError("%s not found" % (_mysql_config_path,))
OSError: mysql_config not found
----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.
The command '/bin/sh -c pip install -r /requirements.txt' returned a non-zero code: 1
【问题讨论】:
【参考方案1】:在 apt-get install 之后运行 pip install requirements 因为 mysqlclient 需要 libmysqlclient-dev:
您正在使用 apt 包管理器和不兼容的 alpine base linux 映像。我建议在支持apt的debian os中使用python3.7-slim。
FROM python:3.7-slim
MAINTAINER Intersources Inc.
ENV PYTHONUNBUFFERED 1
RUN apt-get update
RUN apt-get install python3-dev default-libmysqlclient-dev gcc -y
COPY ./requirements.txt /requirements.txt
RUN pip install -r /requirements.txt
RUN mkdir /app
WORKDIR /app
COPY ./app /app
RUN adduser -D jeet
USER jeet
如果你确实需要 alpine 像这样修改 Dockerfile:
FROM python:3.7-alpine
MAINTAINER Intersources Inc.
RUN apk update
RUN apk add musl-dev mariadb-dev gcc
RUN pip install mysqlclient
RUN mkdir /app
WORKDIR /app
COPY ./app /app
RUN adduser -D jeet
USER jeet
【讨论】:
如果你想让 apt-get 在镜像中工作,我建议使用 python:3.7-slim 作为基础镜像。您还需要安装 gcc。我更正了答案。以上是关于无法为 Django 和 Mysql 启动 docker 构建的主要内容,如果未能解决你的问题,请参考以下文章