我正在使用
python:3-slim
Docker 映像,并想使用 Pypi 中的 mysqlclient 包,但从 RUN pip install mysqlclient
命令中收到以下错误:
...
Collecting mysqlclient
Downloading mysqlclient-2.2.0.tar.gz (89 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 89.5/89.5 kB 2.5 MB/s eta 0:00:00
Installing build dependencies: started
Installing build dependencies: finished with status 'done'
Getting requirements to build wheel: started
Getting requirements to build wheel: finished with status 'error'
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [27 lines of output]
/bin/sh: 1: pkg-config: not found
/bin/sh: 1: pkg-config: not found
Trying pkg-config --exists mysqlclient
Command 'pkg-config --exists mysqlclient' returned non-zero exit status 127.
Trying pkg-config --exists mariadb
Command 'pkg-config --exists mariadb' returned non-zero exit status 127.
Traceback (most recent call last):
File "/usr/local/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
main()
File "/usr/local/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
return hook(config_settings)
^^^^^^^^^^^^^^^^^^^^^
File "/tmp/pip-build-env-f_fea8lo/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 355, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=['wheel'])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/tmp/pip-build-env-f_fea8lo/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 325, in _get_build_requires
self.run_setup()
File "/tmp/pip-build-env-f_fea8lo/overlay/lib/python3.12/site-packages/setuptools/build_meta.py", line 341, in run_setup
exec(code, locals())
File "<string>", line 154, in <module>
File "<string>", line 48, in get_config_posix
File "<string>", line 27, in find_package_name
Exception: Can not find valid pkg-config name.
Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error
× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.
我的 Dockerfile 看起来像:
FROM python:3.12-slim
RUN pip install mysqlclient
我尝试通过将以下内容添加到上面的 Dockerfile 来安装构建依赖项
RUN pip install mysqlclient
:
RUN apt-get install python3-dev default-libmysqlclient-dev build-essential
但这使得 Docker 镜像超过 700MB。我还尝试将上面的行替换为以下内容,以在安装后删除构建依赖项
mysqlclient
:
RUN apt-get update && \
apt-get dist-upgrade && \
apt-get install -y pkg-config default-libmysqlclient-dev \
build-essential libmariadb-dev && \
pip install mysqlclient && \
apt-get purge -y pkg-config default-libmysqlclient-dev build-essential
但是图像大小仍然超过 500MB。
如何安装
mysqlclient
而不用构建依赖项使我的 docker 映像膨胀?
使用多阶段构建 Dockerfile:
FROM python:3.12 AS python-build
RUN pip install mysqlclient
FROM python:3.12-slim
COPY --from=python-build /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
RUN apt-get update && apt-get install libmariadb3
这首先使用全脂
python:3.12
映像,它确实具有安装和编译 mysqlclient
所需的构建工具。与 Python 的典型情况一样,该包及其依赖项安装在 /usr/local/lib/python*/site-packages
中。
然后再次开始构建,这次使用
python:3.12-slim
图像。 COPY
引用第一个图像并仅复制已安装的 Python 包,包括 mysqlclient
。
最后一行安装libmariadb3
,这是mysqlclient
所必需的,否则你会收到以下错误:
ImportError: libmariadb.so.3: cannot open shared object file: No such file or directory