Docker build ubuntu 无法执行 apt-get update

2019-06-20 17:08:23 +08:00
 MrSheng

这是一个老问题了,但确实没找到原因。

Dockerfile 内容:

FROM ubuntu:xenial
RUN apt-get update && apt-get upgrade -y
RUN apt-get install -y net-tools
CMD ifconfig

build 时报错:

Step 1/4 : FROM ubuntu:xenial
 ---> 13c9f1285025
Step 2/4 : RUN apt-get update && apt-get upgrade -y
 ---> Running in 7d9b2ce81413
Err:1 http://security.ubuntu.com/ubuntu xenial-security InRelease
  Temporary failure resolving 'security.ubuntu.com'
Err:2 http://archive.ubuntu.com/ubuntu xenial InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:3 http://archive.ubuntu.com/ubuntu xenial-updates InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:4 http://archive.ubuntu.com/ubuntu xenial-backports InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Reading package lists...
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial-updates/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/xenial-backports/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/xenial-security/InRelease  Temporary failure resolving 'security.ubuntu.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.
Reading package lists...
Building dependency tree...
Reading state information...
Calculating upgrade...
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
 ---> 2bbbf2281fbf
Removing intermediate container 7d9b2ce81413
Step 3/4 : RUN apt-get install -y net-tools
 ---> Running in ce96aba3aea8
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package net-tools
The command '/bin/sh -c apt-get install -y net-tools' returned a non-zero code: 100

很明显是 apt-get 无法 update

已经尝试的解决方式:

1、更改为国内源(阿里源与 163 源,也测试过 sed 直接修改),无效:

RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak && \
echo 'deb http://mirrors.163.com/debian/ jessie main non-free contrib' > /etc/apt/sources.list && \
echo 'deb http://mirrors.163.com/debian/ jessie-updates main non-free contrib' >> /etc/apt/sources.list && \
echo 'deb http://mirrors.163.com/debian-security/ jessie/updates main non-free contrib' >> /etc/apt/sources.list

报错原因同之前:

Err:1 http://mirrors.163.com/debian jessie InRelease
  Temporary failure resolving 'mirrors.163.com'

2、尝试更改 DNS ( 1 )我需要在 docker build 时指定 DNS,不是 run 的时候 ( 2 ) DNS 中已经有 114.114.114.114

PS:宿主机可以执行 apt-update

8472 次点击
所在节点    Docker
3 条回复
XSG
2019-07-01 16:34:23 +08:00
你的源错了
看下你 FROM 的镜像是 xenial,但是修改的 apt 源是 jessie,不匹配!
pacoxu
2019-07-02 13:21:30 +08:00
https://github.com/pacoxu/dao-tomcat/tree/master/samples

写了个简单的例子,找了个 ubuntu 的 aliyun 源

docker build .
Sending build context to Docker daemon 3.584kB
Step 1/3 : FROM ubuntu:xenial
xenial: Pulling from library/ubuntu
Digest: sha256:a4d8e674ee993e5ec88823391de828a5e9286a1597b731eaecaaf9066cfdf539
Status: Downloaded newer image for ubuntu:xenial
---> 13c9f1285025
Step 2/3 : COPY sources.list /etc/apt/sources.list
---> 93899785ec2a
Step 3/3 : RUN apt-get update && apt-get upgrade -y && rm -rf /var/lib/apt/lists/*
---> Running in 2369ec47a573
Get:1 http://mirrors.aliyun.com/ubuntu xenial InRelease [247 kB]
Get:2 http://mirrors.aliyun.com/ubuntu xenial-security InRelease [109 kB]
Get:3 http://mirrors.aliyun.com/ubuntu xenial-updates InRelease [109 kB]
MrSheng
2019-09-23 09:52:59 +08:00
最后解决的方法是 docker build 指定 --network=host
问题中已经说明:宿主机中可以 update,容器中不可以,应该是 docker 配置问题

PS:
更改源版本不对是复制的代码,实际上是正确的版本

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

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

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

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

© 2021 V2EX