【问题标题】:Not able to install packages in docker containers无法在 docker 容器中安装软件包
【发布时间】:2016-10-28 10:04:00
【问题描述】:

以下是我系统中的一些 docker 镜像:

root@labadmin-VirtualBox:/home/labadmin# docker images
REPOSITORY   TAG     IMAGE ID            CREATED               SIZE
ubuntu      14.04   1e0c3dd64ccd        13 days ago         187.9 MB
ubuntu     latest   45bc58500fa3        5 weeks ago         126.9 MB

我想在容器中安装“smartmontools”。但它抛出错误“无法找到包 smartmontools”,如下所示:

root@labadmin-VirtualBox:/home/labadmin# docker run -it 1e0c3dd64ccd
root@b4954826a227:/# apt-get install smartmontools
Reading package lists... Done
Building dependency tree       
Reading state information... Done
E: Unable to locate package smartmontools
root@b4954826a227:/# exit
exit

但是当我在 Ubuntu 机器上做同样的事情时,它就可以工作了。

root@labadmin-VirtualBox:/home/labadmin# apt-get install smartmontools
Reading package lists... Done
Building dependency tree       
Reading state information... Done
***smartmontools is already the newest version.***
0 upgraded, 0 newly installed, 0 to remove and 542 not upgraded.
root@labadmin-VirtualBox:/home/labadmin# 

Ubuntu Containers 和 Ubuntu 系统有什么区别?是什么阻止了软件包安装在容器中?

我的要求是创建一个带有一些实用程序的容器,并以 Ubuntu OS 作为基础映像:

FROM ubuntu:14.04
RUN apt-get update && apt-get install -y smartmontools

【问题讨论】:

  • 容器内是否可以上网?
  • 是的,工作正常,但看起来像 DNS 问题。
  • 你的发现是什么?
  • 更新了以下帖子中的详细过程以解决问题:谢谢

标签: ubuntu docker containers dockerfile


【解决方案1】:

当您通过手动运行容器进行测试时,您不会更新缓存,apt-get update 因此出现Unable to locate package 错误

但是您的 Dockerfile 示例应该可以工作

【讨论】:

  • root@labadmin-VirtualBox:~/RAGHU# docker build -t mont:1.1 。将构建上下文发送到 Docker 守护进程 209.5 MB 第 1 步:FROM ubuntu:14.04 ---> 1e0c3dd64ccd 第 2 步:运行 apt-get update && apt-get install -y smartmontools ---> 在 616f62c55440 中运行更新/InRelease 读取包列表。 .. 构建依赖关系树... 读取状态信息... E: 无法找到包 smartmontools 命令 '/bin/sh -c apt-get update && apt-get install -y smartmontools' 返回了一个非零码:100
  • 尝试使用 --no-cache 和 --pull 进行构建以确保。这将确保图像是最新的,并使可能包含无效数据的缓存失效
【解决方案2】:

以下过程帮助我解决了这个问题:

root@labadmin-VirtualBox:/home/labadmin# docker run busybox ping -c 2 192.203.230.10
PING 192.203.230.10 (192.203.230.10): 56 data bytes
64 bytes from 192.203.230.10: seq=0 ttl=56 time=66.724 ms
64 bytes from 192.203.230.10: seq=1 ttl=56 time=54.786 ms
--- 192.203.230.10 ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 45.815/56.947/66.724 ms

当您尝试使用容器 ping 到 google.com 时,由于 DNS 问题而无法访问。

root@labadmin-VirtualBox:/home/labadmin# docker run busybox nslookup google.com
Server:    8.8.8.8
Address 1: 8.8.8.8

nslookup: can't resolve 'google.com'

找出您机器中使用的 DNS 服务器:

root@labadmin-VirtualBox:/home/labadmin# nm-tool  |grep DNS
    DNS:             172.24.100.50
    DNS:             10.1.100.50

通过添加 DNS IP 执行相同的操作:

root@labadmin-VirtualBox:/home/labadmin# docker run --dns 172.24.100.50 busybox nslookup google.com
Server:    172.24.100.50
Address 1: 172.24.100.50 indc01.radisys.com

Name:      google.com
Address 1: 2607:f8b0:4009:80c::200e ord36s01-in-x0e.1e100.net
Address 2: 172.217.4.110 ord36s04-in-f14.1e100.net

要永久解决它,请将以下内容添加到新文件中:

root@labadmin-VirtualBox:/home/labadmin# cat /etc/docker/daemon.json
{
    "dns" : ["172.24.100.50", "8.8.8.8"]
}

有关 Docker DNS 配置的更多信息:https://docs.docker.com/engine/userguide/networking/configure-dns/

重启docker服务:

root@labadmin-VirtualBox:/home/labadmin# sudo service docker restart
docker stop/waiting
docker start/running, process 22291

root@labadmin-VirtualBox:/home/labadmin# docker run busybox nslookup google.com
Server:    172.24.100.50
Address 1: 172.24.100.50 indc01.radisys.com

Name:      google.com
Address 1: 2607:f8b0:4009:801::200e ord30s31-in-x0e.1e100.net
Address 2: 172.217.4.238 ord30s31-in-f14.1e100.net

通过运行容器检查:

root@labadmin-VirtualBox:/home/labadmin# docker run -it e02e811dd08f
/ # ping google.com
PING google.com (172.217.4.238): 56 data bytes
64 bytes from 172.217.4.238: seq=0 ttl=47 time=251.506 ms
64 bytes from 172.217.4.238: seq=1 ttl=47 time=245.621 ms
^C
--- google.com ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max = 245.621/257.113/272.586 ms
/ #

欲了解更多信息:https://robinwinslow.uk/2016/06/23/fix-docker-networking-dns/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-18
    • 1970-01-01
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 2022-09-23
    • 2018-03-06
    相关资源
    最近更新 更多