docker基础镜像构建phpy踩坑记

2024-09-10 21:01:07 浏览数 (3)

背景

哈哈,说起来也是挺逗的,在这个AI时代,是个程序员都得变身成全能学霸,不然分分钟就被时代的巨轮给甩飞了。我呢?作为一个小菜鸟,心里头那个急啊,生怕一不小心就被AI给“智能”掉了。

结果呢,还没跟AI大战三百回合,就先被自己的电脑配置环境给“虐”了。简直就是一场没有硝烟的战争,安装包、驱动、兼容性问题……想骂人喽!

但话说回来,这过程虽然坎坷,却也乐在其中。毕竟,哪个程序员不是在不断试错中成长的呢?

所以啊,虽然被本地环境虐得有点惨,但我也算是痛并快乐着吧。毕竟,每一次的失败都是向成功迈进的一步嘛!接下来听我来说说,是怎么一步一步被虐的!!!

开始被虐

phpy官方镜像

当我按照phpy官方镜像phpswoole/phpy:1.0.5安装torch

代码语言:javascript复制
pip install torch torchvision torchaudio

出现以下的错误信息

代码语言:javascript复制
ERROR: Could not find a version that satisfies the requirement torch (from versions: none)
ERROR: No matching distribution found for torch

被虐待中....这个错误信息表明你的Python环境在尝试安装PyTorch(一个流行的机器学习库)时未能找到任何合适的版本。

通过AI也搜索到了问题的解决办法

下来我们通过指定版本,实际上如果同时确定torch版本以及适合的CUDA版本,应该使用下面的指令:

代码语言:javascript复制
pip install torch==2.2.0 cpu torchvision torchaudio -f https://download.pytorch.org/whl/torch_stable.html

使用这条指令,可以直接在官方对应的网站检索相应版本下载。可以看出是按照成功了

代码语言:javascript复制
(venv) /var/www # pip install torch==2.2.0 cpu torchvision torchaudio -f https://download.pytorch.org/whl/torch_stable.html
Looking in links: https://download.pytorch.org/whl/torch_stable.html
Collecting torch==2.2.0 cpu
  Downloading https://download.pytorch.org/whl/cpu/torch-2.2.0+cpu-cp312-cp312-linux_x86_64.whl (186.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 186.7/186.7 MB 4.3 MB/s eta 0:00:00
Collecting torchvision
  Downloading https://download.pytorch.org/whl/rocm6.0/torchvision-0.18.1+rocm6.0-cp312-cp312-linux_x86_64.whl (65.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 65.7/65.7 MB 5.5 MB/s eta 0:00:00
Collecting torchaudio
  Downloading https://download.pytorch.org/whl/rocm6.0/torchaudio-2.3.1+rocm6.0-cp312-cp312-linux_x86_64.whl (1.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.7/1.7 MB 5.1 MB/s eta 0:00:00
Collecting filelock (from torch==2.2.0 cpu)
  Downloading filelock-3.15.4-py3-none-any.whl.metadata (2.9 kB)
Collecting typing-extensions>=4.8.0 (from torch==2.2.0 cpu)
  Downloading typing_extensions-4.12.2-py3-none-any.whl.metadata (3.0 kB)
Collecting sympy (from torch==2.2.0 cpu)
  Downloading sympy-1.13.2-py3-none-any.whl.metadata (12 kB)
Collecting networkx (from torch==2.2.0 cpu)
  Downloading networkx-3.3-py3-none-any.whl.metadata (5.1 kB)
Collecting jinja2 (from torch==2.2.0 cpu)
  Downloading jinja2-3.1.4-py3-none-any.whl.metadata (2.6 kB)
Collecting fsspec (from torch==2.2.0 cpu)
  Downloading fsspec-2024.6.1-py3-none-any.whl.metadata (11 kB)
Collecting numpy (from torchvision)
  Downloading numpy-2.1.0-cp312-cp312-musllinux_1_1_x86_64.whl.metadata (60 kB)
INFO: pip is looking at multiple versions of torchvision to determine which version is compatible with other requirements. This could take a while.
Collecting torchvision
  Downloading https://download.pytorch.org/whl/rocm5.7/torchvision-0.18.1+rocm5.7-cp312-cp312-linux_x86_64.whl (65.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 65.5/65.5 MB 1.6 MB/s eta 0:00:00
  Downloading https://download.pytorch.org/whl/cu121/torchvision-0.18.1+cu121-cp312-cp312-linux_x86_64.whl (7.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.0/7.0 MB 2.2 MB/s eta 0:00:00
  Downloading https://download.pytorch.org/whl/cu118/torchvision-0.18.1+cu118-cp312-cp312-linux_x86_64.whl (6.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.3/6.3 MB 2.9 MB/s eta 0:00:00
  Downloading https://download.pytorch.org/whl/cpu/torchvision-0.18.1+cpu-cp312-cp312-linux_x86_64.whl (1.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 3.0 MB/s eta 0:00:00
  Downloading https://download.pytorch.org/whl/rocm6.0/torchvision-0.18.0+rocm6.0-cp312-cp312-linux_x86_64.whl (65.7 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 65.7/65.7 MB 5.0 MB/s eta 0:00:00
  Downloading https://download.pytorch.org/whl/rocm5.7/torchvision-0.18.0+rocm5.7-cp312-cp312-linux_x86_64.whl (65.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 65.5/65.5 MB 5.3 MB/s eta 0:00:00
  Downloading https://download.pytorch.org/whl/cu121/torchvision-0.18.0+cu121-cp312-cp312-linux_x86_64.whl (7.0 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 7.0/7.0 MB 5.4 MB/s eta 0:00:00
INFO: pip is still looking at multiple versions of torchvision to determine which version is compatible with other requirements. This could take a while.
  Downloading https://download.pytorch.org/whl/cu118/torchvision-0.18.0+cu118-cp312-cp312-linux_x86_64.whl (6.3 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.3/6.3 MB 2.5 MB/s eta 0:00:00
  Downloading https://download.pytorch.org/whl/cpu/torchvision-0.18.0+cpu-cp312-cp312-linux_x86_64.whl (1.6 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.6/1.6 MB 4.7 MB/s eta 0:00:00
  Downloading https://download.pytorch.org/whl/rocm5.7/torchvision-0.17.2+rocm5.7-cp312-cp312-linux_x86_64.whl (65.5 MB)
     ━━━━━━━━━━━━━━━━━━━━━╺━━━━━━━━━━━━━━━━━━ 35.1/65.5 MB 1.5 MB/s eta 0:00:20
Downloading certifi-2024.8.30-py3-none-any.whl (167 kB)
Downloading charset_normalizer-3.3.2-cp312-cp312-musllinux_1_1_x86_64.whl (142 kB)
Downloading idna-3.8-py3-none-any.whl (66 kB)
Downloading MarkupSafe-2.1.5-cp312-cp312-musllinux_1_1_x86_64.whl (33 kB)
Downloading mpmath-1.3.0-py3-none-any.whl (536 kB)
   ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 536.2/536.2 kB 3.9 MB/s eta 0:00:00
Downloading urllib3-2.2.2-py3-none-any.whl (121 kB)
Installing collected packages: mpmath, urllib3, typing-extensions, sympy, pillow, numpy, networkx, MarkupSafe, idna, fsspec, filelock, charset-normalizer, certifi, requests, jinja2, torch, torchvision, torchaudio
Successfully installed MarkupSafe-2.1.5 certifi-2024.8.30 charset-normalizer-3.3.2 filelock-3.15.4 fsspec-2024.6.1 idna-3.8 jinja2-3.1.4 mpmath-1.3.0 networkx-3.3 numpy-2.1.0 pillow-10.4.0 requests-2.32.3 sympy-1.13.2 torch-2.2.0 cpu torchaudio-2.2.0 rocm5.7 torchvision-0.17.0 rocm5.7 typing-extensions-4.12.2 urllib3-2.2.2     

opencv-python

接下来我们安装opencv-python这个库

代码语言:javascript复制
pip install opencv-python

安装错误信息

代码语言:javascript复制
# pip install opencv-python
Collecting opencv-python
  Using cached opencv-python-4.10.0.84.tar.gz (95.1 MB)
  Installing build dependencies ... done
  Getting requirements to build wheel ... error
  error: subprocess-exited-with-error

  × Getting requirements to build wheel did not run successfully.
  │ exit code: 1
  ╰─> [33 lines of output]
      Traceback (most recent call last):
        File "/path/to/venv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
          main()
        File "/path/to/venv/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 "/path/to/venv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 112, in get_requires_for_build_wheel
          backend = _build_backend()
                    ^^^^^^^^^^^^^^^^
        File "/path/to/venv/lib/python3.12/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 77, in _build_backend
          obj = import_module(mod_path)
                ^^^^^^^^^^^^^^^^^^^^^^^
        File "/usr/lib/python3.12/importlib/__init__.py", line 90, in import_module
          return _bootstrap._gcd_import(name[level:], package, level)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
        File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
        File "<frozen importlib._bootstrap>", line 1310, in _find_and_load_unlocked
        File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
        File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
        File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
        File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
        File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
        File "<frozen importlib._bootstrap_external>", line 995, in exec_module
        File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
        File "/tmp/pip-build-env-tj3_9lus/overlay/lib/python3.12/site-packages/setuptools/__init__.py", line 16, in <module>
          import setuptools.version
        File "/tmp/pip-build-env-tj3_9lus/overlay/lib/python3.12/site-packages/setuptools/version.py", line 1, in <module>
          import pkg_resources
        File "/tmp/pip-build-env-tj3_9lus/overlay/lib/python3.12/site-packages/pkg_resources/__init__.py", line 2172, in <module>      
          register_finder(pkgutil.ImpImporter, find_on_path)
                          ^^^^^^^^^^^^^^^^^^^
      AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?
      [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.

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

错误信息

代码语言:javascript复制
  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.

这个错误是由于 Python 3.12 中的`pkgutil`模块与某些旧版本的`setuptools`或其他依赖包不兼容所导致的。在 Python 3.12 中,`pkgutil.ImpImporter`已经被移除,这导致了`AttributeError`。

解决方案

  1. 升级 setuptools :pip install --upgrade setuptools
  2. 清除 pip 的缓存:pip cache purge
  3. 其他N种解决方案都不行

被虐待中....结论是:解决不了!解决不了!解决不了!我放弃了。时间不早了

构建自己的phpy镜像

由于官方默认镜像库基础镜像是使用Base image php:8.2-alpine,会有各种不兼容问题。

所以这里拉取官方仓库重新构建,使用基础镜像Base image php:8.2.13-cli-bullseye

代码语言:javascript复制
git clone git@github.com:swoole/phpy.git

构建

代码语言:javascript复制
docker build -t tinywan/docker-php-phpy:8.2.13 .

img 运行容器

代码语言:javascript复制
docker run -it -v d:/dnmp/www/ai:/opt tinywan/docker-php-phpy:8.2.13

进入容器

代码语言:javascript复制
docker exec -it fab440ffc331ca9d47f09 sh

接下来我们安装opencv-python这个库

代码语言:javascript复制
apt install pip
pip install opencv-python

可以看到很快速的安装成功了

基础镜像版本区别

版本

说明

案例

alpine

Alpine Linux 操作系统,它是一个独立发行版本,相比较 Debian 操作系统来,更加轻巧

python:3.11.4-alpine

slim

基于Debian操作系统最小操作系统,删除了许多非必需的软件包,优化了体积

python:3.11.4-slim

bookworm

Debian 12

python:3.11.4-bookworm

bullseye

Debian 11

python:3.11.4-bullseye

通过docker images | sort -k7 -h 比较镜像大小

代码语言:javascript复制
docker pull --quiet python:3.11.4
docker pull --quiet python:3.11.4-slim
docker pull --quiet python:3.11.4-alpine
docker pull --quiet python:3.11.4-bookworm
docker pull --quiet python:3.11.4-slim-bookworm

0 人点赞