在 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM