Loading...

在 docker 中运行 apt-get update 报错 returned a non-zero code: 100

在构建 docker 执行 Dockerfile 中的 RUN apt-get update 时报错 returned a non-zero code: 100。其大致错误内容如下格式:

Get:36 http://ftp.uni-stuttgart.de/debian sid/main amd64 Packages.diff/Index [27.9 kB]
Get:36 http://ftp.uni-stuttgart.de/debian sid/main amd64 Packages.diff/Index [27.9 kB]
Get:37 http://ftp.uni-stuttgart.de/debian sid/main amd64 Packages [9,795 kB]
Reading package lists...
E: Could not open file /var/lib/apt/lists/http.debian.net_debian_dists_sid_main_binary-amd64_Packages.diff_Index - open (2: No such file or directory)
The command '/bin/sh -c apt-get update' returned a non-zero code: 100

解法 1:失败重新 apt-get

查阅资料以为是网络被墙的问题,但没有优雅的解决方法。使用英文查阅后在 github 上找到解法:

FWIW, I was able to work around this problem by adding RUN apt-get update || apt-get update to my Dockerfile.

其中双竖线的意义是:

双竖线‘||’,用双竖线‘||’分割的多条命令,执行的时候遵循如下规则,如果前一条命令为真,则后面的命令不会执行,如果前一条命令为假,则继续执行后面的命令。

apt-get update 失败后重试一次。虽不知为何这么做但很奇妙地解决了报错问题。

解法 2:更换 apt 源

方法 1 后来仍然报错,可能是 DNS 配置的问题,更换成阿里源仍然报错,换成清华源就好了。

# 原本的 Dockerfile
RUN apt-get update

# 更改的 Dockerfile
RUN sed -i "s@/archive.ubuntu.com/@/mirrors.tuna.tsinghua.edu.cn/@g" /etc/apt/sources.list \
    && rm -Rf /var/lib/apt/lists/* \
    && apt-get update

解法 3:再换 apt 源

后又反复报错。查阅 资料 有言:

原因竟然是一些不靠谱的网络供应商喜欢自作聪明,从自己的缓存里寻找你要访问的数据,用来降低带宽,但技术考虑不周期,导致用户使用时出现错误。

特别的,我深有体会的一点是,宽带通/长城宽带特别喜欢搞这些事情,喜欢拦截、修改用户访问的网页,然后在里面添加自己的广告用来盈利,如果你在使用宽带通/长城宽带浏览网页时,网页底部突然弹出广告,这很有可能就是宽带通/长城宽带这些宽带服务商干的恶心事。

同样是 Hash Sum mismatch 的错误,在国内,也变成了具有中国特色的 Hash Sum mismatch 错误和具有中国特殊的解决方案。

再仔细查看自己的报错信息:

....
Fetched 138 MB in 4min 31s (508 kB/s)
Unable to correct missing packages.
E: Failed to fetch http://202.194.1.97/cache/3/01/security.ubuntu.com/e2473394af9ebe243013cfde24eb089c/libpython3.6-minimal_3.6.9-1~18.04ubuntu1.3_amd64.deb  Could not connect to 202.194.1.97:80 (202.194.1.97), connection timed out
E: Failed to fetch http://202.194.1.97/cache/1/01/security.ubuntu.com/dffd69b81f44ab30d761913f119c3b83/python3.6-minimal_3.6.9-1~18.04ubuntu1.3_amd64.deb  Unable to connect to 202.194.1.97:http:
E: Failed to fetch http://mirrors.tuna.tsinghua.edu.cn/ubuntu/pool/main/libb/libbsd/libbsd0_0.8.7-1ubuntu0.1_amd64.deb  Hash Sum mismatch
   Hashes of expected file:
    - SHA512:437f559db8e0f8c3c9b9f406a07e4cd887117fab0714bbbe4e1b3b4fadfdba5377960c867ea03ad0d393080f9d591e8ea317ff65c4572d87ad86778fceb54d95
    - SHA256:a1e1900a51b56919113ac07ad873c25ad0f57a7042a054e5ed755d5d82871745
    - SHA1:933de54aeeb103f77470f1732000d402e97b2f37 [weak]
    - MD5Sum:7100d3629c213ff8bb9a1dfccdc20230 [weak]
    - Filesize:41560 [weak]
   Hashes of received file:
    - SHA512:2b4bf3d1b1eca21d74e2453ce9e9c4d47b41256d00abf0a182a67db6a66b3d094e347ea9981f9e7dea2d807de5a19e8fdaf219f2aac1ae58ffec2e34706473b9
    - SHA256:430aca4e43e02edc5d7e61483a19b270df0d9a593248402ee660ca56cacc5742
    - SHA1:4704ceaae0fb4b4ecd724fc3d4176abc794787b8 [weak]
    - MD5Sum:7ee53125582ff1d70e0ab8fb07bc4094 [weak]
    - Filesize:41560 [weak]
   Last modification reported: Mon, 20 Jan 2020 15:38:15 +0000
E: Failed to fetch http://202.194.1.97/cache/1/01/security.ubuntu.com/659499fbe5d8c3fd28c4c1f3d59c14de/libavcodec57_3.4.8-0ubuntu0.2_amd64.deb  Unable to connect to 202.194.1.97:http:
E: Failed to fetch http://202.194.1.97/cache/3/01/mirrors.tuna.tsinghua.edu.cn/1e79b02bbe4845b67e693084c85ae20c/libepoxy0_1.4.3-1_amd64.deb  Unable to connect to 202.194.1.97:http:
E: Failed to fetch http://202.194.1.97/cache/3/01/mirrors.tuna.tsinghua.edu.cn/cb529471abc33f4c232dfbf647eb212c/libhdf5-100_1.10.0-patch1+docs-4_amd64.deb  Unable to connect to 202.194.1.97:http:
E: Failed to fetch http://mirrors.tuna.tsinghua.edu.cn/ubuntu/pool/universe/libk/libkml/libkmldom1_1.3.0-5_amd64.deb  Hash Sum mismatch
   Hashes of expected file:
    - SHA256:39fc5be774d651ae93177e42a2e5b03856c16dbb7af7e9228a63f60f5c178e13
    - SHA1:ebf235b2ec56e10897b5b674ff44ad3c155a5920 [weak]
    - MD5Sum:e9f7f92be9d3657ba39579b395538e85 [weak]
    - Filesize:165300 [weak]
   Hashes of received file:
    - SHA256:17ef6fe7d104656518e58b2c47d27b2067cbd5ef1b2da98acf7fc9d154c601bb
    - SHA1:e20a4402d4196efd80e1aae5dfc04557399d47fa [weak]
    - MD5Sum:63db65e57c348df954d2f6f1b1231fd8 [weak]
    - Filesize:165300 [weak]
   Last modification reported: Thu, 02 Nov 2017 20:52:33 +0000
E: Failed to fetch http://mirrors.tuna.tsinghua.edu.cn/ubuntu/pool/universe/libk/libkml/libkmlengine1_1.3.0-5_amd64.deb  Hash Sum mismatch
   Hashes of expected file:
    - SHA256:068f89990dd337f7481586bd37867b56f9e7e09b93cf4b5fe11186c77025bbb4
    - SHA1:279c8709198c2090d7f261385ca65914ee14a657 [weak]
    - MD5Sum:4707732ded16ccaa2b1795617eb51477 [weak]
    - Filesize:73672 [weak]
   Hashes of received file:
    - SHA256:cdffb31fe79216afd2b471306622ff67bda4a62f3e03ee73e1ec9d3ad33b96dc
    - SHA1:8ab8c2dbf84991316332f1520e4534faddec871f [weak]
    - MD5Sum:24ae3353582220fc8f2cb37437538a56 [weak]
    - Filesize:73672 [weak]
   Last modification reported: Thu, 02 Nov 2017 20:52:33 +0000
E: Failed to fetch http://202.194.1.97/cache/4/01/mirrors.tuna.tsinghua.edu.cn/43da4ee09ea8e84f9ef9177f8295eed2/libgdal20_2.2.3+dfsg-2_amd64.deb  Unable to connect to 202.194.1.97:http:
E: Failed to fetch http://202.194.1.97/cache/2/01/mirrors.tuna.tsinghua.edu.cn/723044ca84b6d1c2b3edb2c0fd8cd403/libopenmpi2_2.1.1-8_amd64.deb  Unable to connect to 202.194.1.97:http:
E: Failed to fetch http://202.194.1.97/cache/9/01/mirrors.tuna.tsinghua.edu.cn/d54fe88cd514f06913e2eb6a5c4fc650/libhdf5-openmpi-100_1.10.0-patch1+docs-4_amd64.deb  Unable to connect to 202.194.1.97:http:
E: Failed to fetch http://202.194.1.97/cache/6/01/security.ubuntu.com/03917bcfdba9ac3ce4673b912082c4cd/libpython2.7-stdlib_2.7.17-1~18.04ubuntu1.2_amd64.deb  Unable to connect to 202.194.1.97:http:
E: Failed to fetch http://202.194.1.97/cache/1/01/mirrors.tuna.tsinghua.edu.cn/578269ee1f49c020afcc45f009945df2/libtcl8.6_8.6.8+dfsg-3_amd64.deb  Unable to connect to 202.194.1.97:http:
E: Failed to fetch http://202.194.1.97/cache/9/01/mirrors.tuna.tsinghua.edu.cn/1e5b62f3d245e416a99a04accd5b3408/libtk8.6_8.6.8-4_amd64.deb  Unable to connect to 202.194.1.97:http:
E: Failed to fetch http://202.194.1.97/cache/5/01/mirrors.tuna.tsinghua.edu.cn/72f144c2f3a8c30bc66cc86f65310065/libxt6_1.1.5-1_amd64.deb  Unable to connect to 202.194.1.97:http:
E: Failed to fetch http://202.194.1.97/cache/9/01/mirrors.tuna.tsinghua.edu.cn/ff77053012a419a45f3eb343e49d020c/libtesseract4_4.00~git2288-10f4998a-2_amd64.deb  Unable to connect to 202.194.1.97:http:
E: Failed to fetch http://202.194.1.97/cache/3/01/security.ubuntu.com/f5135970d5207d9bb95773b73e11ada1/libpython3.6_3.6.9-1~18.04ubuntu1.3_amd64.deb  Unable to connect to 202.194.1.97:http:
E: Failed to fetch http://202.194.1.97/cache/12/01/security.ubuntu.com/58f66e7a99bf7c5b88c1e23ddc42ec45/libpython3.6-dev_3.6.9-1~18.04ubuntu1.3_amd64.deb  Unable to connect to 202.194.1.97:http:
E: Aborting install.
The command '/bin/sh -c apt-get install -y git --fix-missing     python3-opencv ca-certificates python3-dev git wget sudo     && rm -rf /var/lib/apt/lists/*     && ln -sv /usr/bin/python3 /usr/bin/python     && useradd -m --no-log-init --system  --uid ${USER_ID} appuser -g sudo     && echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers' returned a non-zero code: 100

可以看到,其下载文件时没有从源站下载,而是从特定网站的 cache 目录下获取缓存文件。解决方法有二,一是修改网络线路,二是再换源。咱使用第二种,再次更换为了网易源:

RUN sed -i "s@/archive.ubuntu.com/@/mirrors.163.com/@g" /etc/apt/sources.list \
    && rm -Rf /var/lib/apt/lists/* \
    && apt-get update

即成功了。

解法 4:终解

上述方法在重复使用后,由于校园网的缓存,再次失效。不使用缓存就不会报错了!为 apt-get update 添加 -o Acquire::http::No-Cache=True 参数即可:

RUN sed -i "s@/archive.ubuntu.com/@/mirrors.163.com/@g" /etc/apt/sources.list \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get update --fix-missing -o Acquire::http::No-Cache=True

参考

[1]apt-get update' returned a non-zero code: 100 #19 - Github
[2]docker apt-get 换源问题 - Laravel
[3]在Linux上执行apt-get update时出现 Hash Sum mismatch 错误的解决方法
[4]APT Hash sum mismatch错误的常见解决方法总结 - CSDN

posted @ 2020-10-15 17:03  板子~  阅读(11320)  评论(0编辑  收藏  举报