在Docker容器中apt-get命令无法工作

8
我正在尝试在 Docker 镜像中安装一些软件包,但是 apt-get 命令似乎不起作用... 当我在基于 ubuntu:16.04 的容器中尝试时。
apt-get update && apt-get install -y openssh-server

我得到了这个:
Err:1 http://archive.ubuntu.com/ubuntu zesty InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:2 http://security.ubuntu.com/ubuntu zesty-security InRelease
  Temporary failure resolving 'security.ubuntu.com'
Err:3 http://archive.ubuntu.com/ubuntu zesty-updates InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:4 http://archive.ubuntu.com/ubuntu zesty-backports InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Reading package lists... Done
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/zesty/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/zesty-updates/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/zesty-backports/InRelease  Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/zesty-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... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package openssh-server

我尝试在Dockerfile中实现以下内容:
FROM ubuntu:16.04
RUN apt-get update && apt-get -y install openssh-server
CMD bin/bash

并使用以下内容构建:

sudo docker build -t imagetest .

但是我得到了这个:
Sending build context to Docker daemon  2.048kB
Step 1/3 : FROM ubuntu:16.04
 ---> 00fd29ccc6f1
Step 2/3 : RUN apt-get update && apt-get install openssh-server
 ---> Running in ee011c1239d0
Err:1 http://archive.ubuntu.com/ubuntu xenial InRelease
  Temporary failure resolving 'archive.ubuntu.com'
Err:2 http://security.ubuntu.com/ubuntu xenial-security InRelease
  Temporary failure resolving 'security.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...
E: Unable to locate package openssh-server
The command '/bin/sh -c apt-get update && apt-get install openssh-server' returned a non-zero code: 100

我在官方文档和一些论坛上搜索了一下,但是没有找到有用且更新的答案...部分人说需要重启docker服务,但对我无效。还有一些人说要使用apt-get update -qq,但这对我也不起作用...因此,我来这里直接询问并寻求最近的答案。谢谢。

1
你是否在防火墙代理后面运行? - yamenk
不是,但我在使用具有NAT配置的虚拟机。 - ecavard
尝试切换到桥接选项,看看是否解决了您的问题。 - yamenk
我无法切换到桥接选项,因为我的网络需要SSH密钥验证,而我的虚拟机没有这个功能...你认为这可能是因为这个原因吗?但是我可以在我的虚拟机上使用apt-get命令,在Docker文件或容器中它不起作用。 - ecavard
2个回答

6
如果VM上的apt-get正常工作,您可以使用网络主机模式构建容器。
docker build --network=host ...

0

我成功地解决了相同的问题(可能不是相同的根本原因),方法是在我的 docker 中使用与 apt.conf 相同的代理设置。

只需复制 atp.conf 即可:

cp /etc/apt/apt.conf apt.conf

然后在执行任何 apt-calls 之前,将以下内容添加到 docker-file 中:

COPY apt.conf /etc/apt/apt.conf

我希望它能帮助到某人。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接