Docker build 无法执行 Dockerfile 中的 apt-get update

2017-03-01 09:59:56 +08:00
 yakumo17

文档地址 https://docs.docker.com/engine/getstarted/step_four/

按照文档步骤来的,执行该命令时报错。

试了在 Dockerfile 中加入 apt-get clean 和 sudo rm -vf /var/lib/apt/lists/*,只能减少几条 Get 报错,不能完全解决问题。

我猜测大概是 Ubuntu 的源的问题,不知道如何改 image 的源。

Docker 的设置中已添加过了 Daocloud 的加速器。

Dockerfile 文件

FROM docker/whalesay:latest
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay

命令执行过程

$ docker build -t docker-whale .
Sending build context to Docker daemon 2.048 kB
Step 1/3 : FROM docker/whalesay:latest
 ---> 6b362a9f73eb
Step 2/3 : RUN apt-get -y update && apt-get install -y fortunes
 ---> Running in c4bed37eb3f2
Ign http://archive.ubuntu.com trusty InRelease
Get:1 http://archive.ubuntu.com trusty-updates InRelease [65.9 kB]
Get:2 http://archive.ubuntu.com trusty-security InRelease [65.9 kB]
Hit http://archive.ubuntu.com trusty Release.gpg
Get:3 http://archive.ubuntu.com trusty-updates/main Sources [1103 kB]
Hit http://archive.ubuntu.com trusty Release
Get:4 http://archive.ubuntu.com trusty-updates/universe Sources [220 kB]
Get:5 http://archive.ubuntu.com trusty-updates/main amd64 Packages [1197 kB]
Get:6 http://archive.ubuntu.com trusty-updates/restricted Sources [1103 kB]
Get:7 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [1197 kB]
Get:8 http://archive.ubuntu.com trusty-security/restricted Sources [1103 kB]
Get:9 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [17.0 kB]
Get:10 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [1197 kB]
Get:11 http://archive.ubuntu.com trusty-security/main Sources [2815 B]
Get:12 http://archive.ubuntu.com trusty-security/universe Sources [2815 B]
Get:13 http://archive.ubuntu.com trusty-security/main amd64 Packages [1197 kB]
Hit http://archive.ubuntu.com trusty/restricted Sources
Get:14 http://archive.ubuntu.com trusty-security/universe amd64 Packages [1197 kB]
Get:15 http://archive.ubuntu.com trusty/universe Sources [220 kB]
Get:16 http://archive.ubuntu.com trusty/main Sources [2815 B]
Get:17 http://archive.ubuntu.com trusty/main amd64 Packages [14.8 kB]
Hit http://archive.ubuntu.com trusty/restricted amd64 Packages
Get:18 http://archive.ubuntu.com trusty/universe amd64 Packages [17.0 kB]
Fetched 9920 kB in 17s (580 kB/s)
W: Size of file /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_trusty_main_source_Sources.gz is not what the server reported 2815 1334581
W: Size of file /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_trusty_universe_source_Sources.gz is not what the server reported 220105 7925687
W: Size of file /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_trusty_main_binary-amd64_Packages.gz is not what the server reported 14793 1743009
W: Size of file /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_trusty_universe_binary-amd64_Packages.gz is not what the server reported 17048 7588885
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-updates/main/source/Sources  Hash Sum mismatch

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-updates/restricted/source/Sources  Hash Sum mismatch

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-updates/universe/source/Sources  Hash Sum mismatch

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-updates/main/binary-amd64/Packages  Hash Sum mismatch

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-updates/restricted/binary-amd64/Packages  Hash Sum mismatch

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-updates/universe/binary-amd64/Packages  Hash Sum mismatch

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-security/main/source/Sources  Hash Sum mismatch

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-security/restricted/source/Sources  Hash Sum mismatch

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-security/universe/source/Sources  Hash Sum mismatch

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-security/main/binary-amd64/Packages  Hash Sum mismatch

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty-security/universe/binary-amd64/Packages  Hash Sum mismatch

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty/main/source/Sources  Hash Sum mismatch

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty/universe/source/Sources  Hash Sum mismatch

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty/main/binary-amd64/Packages  Hash Sum mismatch

W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/trusty/universe/binary-amd64/Packages  Hash Sum mismatch

E: Some index files failed to download. They have been ignored, or old ones used instead.
The command '/bin/sh -c apt-get -y update && apt-get install -y fortunes' returned a non-zero code: 100
10296 次点击
所在节点    问与答
8 条回复
Eleutherios
2017-03-01 10:13:49 +08:00
……

建议你好好看下 log ,然后去 Google 搜索: “ Hash Sum mismatch ” “ apt-get ”
zjb861107
2017-03-01 10:28:45 +08:00
要翻墙吧,或者用 DockerHub 镜像加速
hxsf
2017-03-01 10:40:11 +08:00
FROM docker/whalesay:latest
RUN 换国内源
RUN apt-get -y update && apt-get install -y fortunes
CMD /usr/games/fortune -a | cowsay
yakumo17
2017-03-01 11:15:24 +08:00
@Eleutherios 搜过的来着,顺着 stackoverflow 一条一条试,在 apt-get update 前加 apt-get clean 等,试过没有效果。在 /etc/apt/sources.list 中加入 deb 命令那些,不知道怎么找到镜像的文件位置,自己的系统是没有这个 /etc/apt/的目录···
yakumo17
2017-03-01 11:17:25 +08:00
@hxsf 找到一条这个命令
RUN sed -i 's/http:\/\/archive\.ubuntu\.com\/ubuntu\//http:\/\/mirrors\.163\.com\/ubuntu\//g' /etc/apt/sources.list
解决了 3q
还不太懂这个语句
yakumo17
2017-03-01 11:18:32 +08:00
@zjb861107 翻墙一直开着的,镜像加速用了,对下载 hub.docker.com 的有用,这里依然不行
wahyd4
2017-03-01 11:22:01 +08:00
解决 apt-get update 慢的问题,你可以试试这个: RUN sudo sed -i 's/archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list
Eleutherios
2017-03-01 11:26:33 +08:00
首先,你需要知道 apt-get update 在干什么

其次,你需要知道 "Hash Sum mismatch" 意味着什么:
archive.ubuntu.com 更新 package 信息失败(被墙干扰) => 所以,你需要使用国内的 repo

然后,你需要知道 sed -i '******' /path/to/file 命令在干什么:
https://archive.ubuntu.com/ 替换为 http://mirrors.163.com/

另外,你在主机开翻墙,很可能对 Docker 是无效的。
如果你要让 Docker 翻墙,你可以 Google socks5 docker

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/343969

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX