我正在使用
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
该错误是由于
slim
镜像没有 pkg-config 和 GCC 等构建工具造成的。
您可以安装 apt-get install 所需的构建工具,但这将使您的图像大大膨胀,即使您随后尝试删除它们。相反,使用多阶段构建 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