最近写了一个python脚本需要在别人做好的docker镜像基础下运行,结果调试的时候就发现弹出这个错误(所有python依赖库均是正常),正常来说如果是自己部署的python尤其是是conda安装的环境,一般就自带了,但因为是要在别人的镜像下进行适配,不可能轻易做这种推倒从来的事情,于是尝试了一下网上说的方法。
apt-get install libjpeg9
然而在该镜像下没办法安装,提示:
E: Unable to locate package libjpeg9
The command '/bin/sh -c : && apt update -y && seq 3 | xargs -i apt-get install -y libjpeg9 && :' returned a non-zero code: 123
后来查了一下镜像的系统版本(下面的docker_image_tag是镜像名代称,需要换成你自己的镜像名称才能执行):
docker run --rm docker_image_tag lsb_release -a
出来的结果是 Debian:
Distributor ID: Debian
Description: Debian GNU/Linux 10 (buster)
Release: 10
Codename: buster
No LSB modules are available.
在Debian 10下是没办法用apt-serch找到的,上述的方法是基于ubuntu的,按按理说也不应该,因为Debian 10应该是自带libjpeg9的,不过搜了一下类似的倒是能搜出其他:
apt search libjpeg.so.9
Sorting... Done
Full Text Search... Done
apt search libjpeg*
Sorting... Done
Full Text Search... Done
libjpedal-jbig2-java/oldstable 20100117-1.1 all
library for accession of large imageslibjpeg-dev/oldstable 1:1.5.2-2+deb10u1 all [upgradable from: 1:1.5.2-2]
Development files for the JPEG library [dummy package]libjpeg-turbo-progs/oldstable 1:1.5.2-2+deb10u1 amd64
Programs for manipulating JPEG fileslibjpeg62-turbo/oldstable 1:1.5.2-2+deb10u1 amd64 [upgradable from: 1:1.5.2-2+b1]
libjpeg-turbo JPEG runtime librarylibjpeg62-turbo-dev/oldstable 1:1.5.2-2+deb10u1 amd64 [upgradable from: 1:1.5.2-2+b1]
Development files for the libjpeg-turbo JPEG library
然后网上搜了一下答案,也有其他人吐槽Debian下没有libjpeg.so.8:https://unix.stackexchange.com/questions/687633/why-is-libjpeg-so-9-missing-from-debian
里面的回复是这样的:
Debian used to include
libjpeg8
, but it was removed in 2017 in favour of libjpeg9. In practice, thelibjpeg
library in Debian is libjpeg62-turbo — that’s the onlylibjpeg
variant provided in Debian 10 and 11 (the decision is documented in #717076).The general rule in Debian is to only carry libraries which are necessary for the distribution itself. Trying to provide all libraries potentially needed for any given binary out there is impossible.
However, old packages don’t disappear entirely, and in many cases they can still be used. The last version of
libjpeg8
published in Debian is available from snapshots, and can be installed on current Debian releases; you’ll also need multiarch-support from snapshots.You could always file a bug asking for a compatibility package, which is the approach taken in Ubuntu, but I’m not sure how well received such a solution would be in Debian.
这样的话,除非是自己编译,否则基本上是行不通的,但我比较懒,第一步想的就是把自己本机ubuntu下的 libjpeg.so.9挪过来看看能不能用。
首先准备好文件。然后以运行容器的方法进入镜像,查看相关目录:
docker run --rm -v 你的服务器路径 你挂载的docker路径 your docker_image_tag sleep 10000
# 以sleep命令run你的docker镜像,使得当前运行起来的容器能挂起,方便你及时进入
docker exec -it 刚才跑起来的镜像id /bin/bash
cd /usr/local/lib
然后发现该目录下什么都没有,可能是镜像做得比较简单,假设你现在已经搞来了libjpeg.so.9文件,并且贴到该目录上,类似以下的效果:
ls -l /usr/local/lib/libjpeg.so.9.5.0
-rw------- 1 root root 267984 Dec 9 13:57 /usr/local/lib/libjpeg.so.9.5.0
只是做到这一步,其实是不够的,因为系统的共享库是在对应的共享库在文件中指引系统找到相关的路径。默认情况下,/etc/ld.so.conf 文件中包含有默认的共享库搜索路径。进入/etc/ld.so.conf文件中看到什么内容没有,所以将/usr/local/bin和/usr/local/lib路径加入:
echo "include /usr/local/lib" >> /etc/ld.so.conf
cat /etc/ld.so.conf
include /etc/ld.so.conf.d/*.confinclude /usr/local/lib
然后要执行ldconfig这个命令,刷新/etc/ld.so.cache中的内容:
ldconfig
之后运行程序就可以解决了。
当然,在上述步骤中,可以将文件改为迁移到你想要的路径,并且在/etc/ld.so.conf 添加上该文件的路径就可,但实际上还是推荐在/etc/ld.so.conf 文件中加入/usr/local/bin和/usr/local/lib。