gpt4 book ai didi

python - 无法为 Django 和 Mysql 启动 docker 构建

转载 作者:行者123 更新时间:2023-11-29 15:15:16 27 4
gpt4 key购买 nike

我正在使用 Djnago 和 MySql 构建一个应用程序。我想使用 docker 来部署我的应用程序。我准备了一个 requirement.txtdocker-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

需求.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

最佳答案

只需在 apt-get install 之后运行 pip install requests,因为 mysqlclient 需要 libmysqlclient-dev:

您使用的 apt 包管理器与 alpine 基本 Linux 镜像不兼容。我建议使用 python3.7-slim 和支持 apt 的 debian 操作系统。

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

关于python - 无法为 Django 和 Mysql 启动 docker 构建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59719430/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com