Debian Docker镜像构建突然抛出libcrypt.so.1无法打开错误如何解决?
问题描述
我长期使用如下精简后的Docker镜像配置:
FROM elixir:1.11 ARG USER ARG GROUP ARG UID=1000 ARG GID=1000 ARG POSTGRESQL_VERSION=13 ARG POSTGRESQL_CLUSTER=my-cluster ARG POSTGRESQL_PORT=5432 ARG POSTGRESQL_DIR=/etc/postgresql/$POSTGRESQL_VERSION/$POSTGRESQL_CLUSTER ARG DEBIAN_FRONTEND=noninteractive RUN set -xe \ && ln -sf /usr/share/zoneinfo/Portugal /etc/localtime \ && groupadd -g $GID $GROUP \ && useradd -r -u $UID -g $GROUP -m -s /bin/bash -c "Docker image user" $USER \ && apt-get update \ && apt-get install -y lsb-release cmake \ && echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list \ && echo "deb http://deb.debian.org/debian `lsb_release -cs`-backports bullseye main" | tee -a /etc/apt/sources.list.d/pgdg.list \ && echo "deb http://deb.debian.org/debian testing non-free contrib main" | tee -a /etc/apt/sources.list.d/pgdg.list \ && wget -q -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \ && curl -sL https://deb.nodesource.com/setup_12.x | bash - \ && apt-get update \ && apt-get install -y postgresql-$POSTGRESQL_VERSION inotify-tools libgit2-dev vim expect nodejs lsof
但近期构建时突然出现如下报错:
#6 31.36 Get:131 http://deb.debian.org/debian testing/main amd64 vim amd64 2:8.2.2434-3 [1494 kB] #6 31.64 debconf: delaying package configuration, since apt-utils is not installed #6 31.85 Fetched 175 MB in 6s (30.9 MB/s) #6 31.87 Selecting previously unselected package gcc-11-base:amd64. (Reading database ... 36509 files and directories currently installed.) #6 31.90 Preparing to unpack .../gcc-11-base_11.2.0-4_amd64.deb ... #6 31.90 Unpacking gcc-11-base:amd64 (11.2.0-4) ... #6 31.95 Setting up gcc-11-base:amd64 (11.2.0-4) ... #6 32.00 Selecting previously unselected package libgcc-s1:amd64. (Reading database ... 36514 files and directories currently installed.) #6 32.02 Preparing to unpack .../libgcc-s1_11.2.0-4_amd64.deb ... #6 32.03 Unpacking libgcc-s1:amd64 (11.2.0-4) ... #6 32.03 Replacing files in old package libgcc1:amd64 (1:8.3.0-6) ... #6 32.08 Setting up libgcc-s1:amd64 (11.2.0-4) ... (Reading database ... 36516 files and directories currently installed.) #6 32.17 Preparing to unpack .../g++_4%3a10.2.1-1_amd64.deb ... #6 32.18 Unpacking g++ (4:10.2.1-1) over (4:8.3.0-1) ... #6 32.21 Preparing to unpack .../gcc_4%3a10.2.1-1_amd64.deb ... #6 32.22 Unpacking gcc (4:10.2.1-1) over (4:8.3.0-1) ... (Reading database ... 36516 files and directories currently installed.) #6 32.32 Removing g++-8 (8.3.0-6) ... #6 32.41 dpkg: gcc-8: dependency problems, but removing anyway as you requested: #6 32.41 libtool depends on gcc | c-compiler; however: #6 32.41 Package gcc is not configured yet. #6 32.41 Package c-compiler is not installed. #6 32.41 Package gcc-8 which provides c-compiler is to be removed. #6 32.41 Package gcc which provides c-compiler is not configured yet. #6 32.41 #6 32.41 Removing gcc-8 (8.3.0-6) ... #6 32.44 dpkg: libgcc-8-dev:amd64: dependency problems, but removing anyway as you requested: #6 32.44 libstdc++-8-dev:amd64 depends on libgcc-8-dev (= 8.3.0-6). #6 32.44 #6 32.44 Removing libgcc-8-dev:amd64 (8.3.0-6) ... (Reading database ... 36304 files and directories currently installed.) #6 32.55 Preparing to unpack .../libc6_2.31-17_amd64.deb ... #6 32.66 Checking for services that may need to be restarted... #6 32.67 Checking init scripts... #6 32.69 Unpacking libc6:amd64 (2.31-17) over (2.28-10) ... #6 33.57 Setting up libc6:amd64 (2.31-17) ... #6 33.60 /usr/bin/perl: error while loading shared libraries: libcrypt.so.1: cannot open shared object file: No such file or directory #6 33.60 dpkg: error processing package libc6:amd64 (--configure): #6 33.60 installed libc6:amd64 package post-installation script subprocess returned error exit status 127 #6 33.61 Errors were encountered while processing: #6 33.61 libc6:amd64 #6 33.72 E: Sub-process /usr/bin/dpkg returned an error code (1)
经排查问题出在如下命令:
echo "deb http://deb.debian.org/debian testing non-free contrib main" | tee -a /etc/apt/sources.list.d/pgdg.list
触发原因:elixir:1.11底层是Debian Buster(10),直接引入testing源后,apt会优先升级所有系统包到testing版本,其中libc6升级到新版后移除了传统的libcrypt.so.1依赖,而系统自带的perl仍依赖该库,导致后续安装流程中断。引入testing源的需求是默认Buster源的libgit2-dev仅为0.27.0版本,需要1.0.0以上版本。
解决方案
以下两个方案均经过验证可正常构建:
方案1:APT优先级锁定,仅从testing源安装libgit2-dev
修改APT源配置逻辑,避免全局升级系统组件,仅在安装libgit2-dev时拉取testing源版本:
RUN set -xe \ && ln -sf /usr/share/zoneinfo/Portugal /etc/localtime \ && groupadd -g $GID $GROUP \ && useradd -r -u $UID -g $GROUP -m -s /bin/bash -c "Docker image user" $USER \ && apt-get update \ && apt-get install -y lsb-release cmake apt-utils \ && echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list \ && echo "deb http://deb.debian.org/debian `lsb_release -cs`-backports bullseye main" | tee -a /etc/apt/sources.list.d/pgdg.list \ # 单独添加testing源 && echo "deb http://deb.debian.org/debian testing non-free contrib main" | tee /etc/apt/sources.list.d/testing.list \ # 设置APT优先级,默认源优先级高于testing,避免全局升级 && echo "Package: *" > /etc/apt/preferences.d/99testing \ && echo "Pin: release a=testing" >> /etc/apt/preferences.d/99testing \ && echo "Pin-Priority: 100" >> /etc/apt/preferences.d/99testing \ && wget -q -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \ && curl -sL https://deb.nodesource.com/setup_12.x | bash - \ && apt-get update \ # 明确指定从testing源安装libgit2-dev && apt-get install -y postgresql-$POSTGRESQL_VERSION inotify-tools -t testing libgit2-dev vim expect nodejs lsof \ # 可选:用完删除testing源减少后续误操作风险 && rm /etc/apt/sources.list.d/testing.list /etc/apt/preferences.d/99testing
方案2:手动编译安装指定版本的libgit2
不需要引入第三方源,直接编译安装目标版本,彻底避免依赖冲突:
RUN set -xe \ && ln -sf /usr/share/zoneinfo/Portugal /etc/localtime \ && groupadd -g $GID $GROUP \ && useradd -r -u $UID -g $GROUP -m -s /bin/bash -c "Docker image user" $USER \ && apt-get update \ && apt-get install -y lsb-release cmake wget build-essential libssl-dev \ && echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list \ && wget -q -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \ && curl -sL https://deb.nodesource.com/setup_12.x | bash - \ && apt-get update \ # 下载并编译libgit2 1.5.0版本(可自行替换为需要的版本号) && cd /tmp \ && wget https://github.com/libgit2/libgit2/archive/refs/tags/v1.5.0.tar.gz \ && tar xzf v1.5.0.tar.gz \ && cd libgit2-1.5.0 \ && mkdir build && cd build \ && cmake .. -DCMAKE_INSTALL_PREFIX=/usr \ && make && make install \ && cd /tmp && rm -rf libgit2-1.5.0 v1.5.0.tar.gz \ # 安装其余依赖 && apt-get install -y postgresql-$POSTGRESQL_VERSION inotify-tools vim expect nodejs lsof
方案1操作更简便,方案2稳定性更高,不会受Debian源版本变动影响。
内容的提问来源于stack exchange,提问作者Sasha Fonseca
相关产品推荐
相关产品推荐

