Dokcer - 为 Django 项目创建图像的问题

问题描述 投票:0回答:1

我正在尝试将 Docker 用于 Django 项目,但是当我尝试构建图像时遇到了问题。 Dockerfile :

# pull the official base image
FROM python:3.8.3-alpine

# set work directory
WORKDIR /usr/src/app

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
RUN pip install --upgrade pip 
COPY ./requirements.txt /usr/src/app
RUN pip install -r requirements.txt

# copy project
COPY . /usr/src/app

EXPOSE 8000

CMD ["python", "manage.py", "runserver", "0.0.0.0:8000"]`

requirements.txt 文件:

asgiref==3.4.1 certifi==2022.12.7 cffi==1.15.0 charset-normalizer==3.1.0 密码学==35.0.0 distlib==0.3.6 Django==3.2.9 django-crispy-forms==1.13.0 django-extensions==3.1.5 django-sslserver==0.22 filelock==3.10.0 idna==3.4 mysqlclient==2.1.1 paho-mqtt==1.6.1 platformdirs==3.1.1 pycparser==2.21 pyOpenSSL==21.0.0 PyQt5==5.15.7 PyQt5-Qt5==5.15.2 PyQt5-sip==12.11.0 pytz==2021.3 requests==2.28.2 six==1.16.0 sqlparse==0.4.2 urllib3==1.26.14 virtualenv==20.21.0 Werkzeug==2.0.2

终端的问题:

  Downloading mysqlclient-2.1.1.tar.gz (88 kB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 88.1/88.1 kB 3.9 MB/s eta 0:00:00
  Preparing metadata (setup.py): started
  Preparing metadata (setup.py): finished with status 'error'
  error: subprocess-exited-with-error

  × python setup.py egg_info did not run successfully.
  │ exit code: 1
  ╰─> [16 lines of output]
      mysql_config --version
      /bin/sh: mysql_config: not found
      mariadb_config --version
      /bin/sh: mariadb_config: not found
      mysql_config --libs
      /bin/sh: mysql_config: not found
      Traceback (most recent call last):
        File "<string>", line 2, in <module>
        File "<pip-setuptools-caller>", line 34, in <module>
        File "/tmp/pip-install-p4jxebcw/mysqlclient_30d2d8c61a2245239a1308ab8f3dd8ea/setup.py", line 15, in <module>
          metadata, options = get_config()
        File "/tmp/pip-install-p4jxebcw/mysqlclient_30d2d8c61a2245239a1308ab8f3dd8ea/setup_posix.py", line 70, in get_config
          libs = mysql_config("libs")
        File "/tmp/pip-install-p4jxebcw/mysqlclient_30d2d8c61a2245239a1308ab8f3dd8ea/setup_posix.py", line 31, in mysql_config
          raise OSError("{} not found".format(_mysql_config_path))
      OSError: mysql_config not found
      [end of output]

  note: This error originates from a subprocess, and is likely not a problem with pip.
error: metadata-generation-failed

× Encountered error while generating package metadata.
╰─> See above for output.

note: This is an issue with the package mentioned above, not pip.
hint: See above for details.
The command '/bin/sh -c pip install -r requirements.txt' returned a non-zero code: 1

here for pic

有人有同样的问题吗?

也许我不应该有 requirements.txt 文件?

python django docker dockerfile docker-image
1个回答
1
投票

OSError: mysql_config not found

运行前需要先安装mysql客户端包

pip.
。在你的泊坞窗中添加一行。

RUN apk add --no-cache mysql-client

大多数 python 包都是纯 python,但它们需要系统库的情况并不少见。这是一个这样的例子,其中必须安装 python 的“外部”东西。

© www.soinside.com 2019 - 2024. All rights reserved.