Docker ubuntu构建在ec2上失败

Docker ubuntu构建在ec2上失败,ubuntu,docker,amazon-ec2,Ubuntu,Docker,Amazon Ec2,非常想了解更多关于Docker的信息。希望在ec2实例上执行我的工作。认为本教程是一个很好的起点: . 当我试图从Dockerfile构建docker映像时,我在运行apt get update-y时出错 最初认为一个不受支持的ubuntu版本(12.04)是一个更新过的问题,以至于参考了最新版本(17.04),但仍然存在与apt更新相关的错误(见下文) 更新:我在非ec2机器上运行了这些步骤,并在apt更新阶段获得了不同的输出,因此我认为ec2配置中的某些东西会干扰网络连接 谢谢你的帮助。控制

非常想了解更多关于Docker的信息。希望在ec2实例上执行我的工作。认为本教程是一个很好的起点: . 当我试图从Dockerfile构建docker映像时,我在运行
apt get update-y
时出错

最初认为一个不受支持的ubuntu版本(12.04)是一个更新过的问题,以至于参考了最新版本(17.04),但仍然存在与apt更新相关的错误(见下文)

更新:我在非ec2机器上运行了这些步骤,并在apt更新阶段获得了不同的输出,因此我认为ec2配置中的某些东西会干扰网络连接

谢谢你的帮助。控制台输出如下:

ec2 docker build --no-cache -t moikaturns/amazon-ecs-sample .
Sending build context to Docker daemon   363 kB
Step 1/12 : FROM ubuntu:17.04
 ---> bde41be8de8c
Step 2/12 : RUN apt-get update -y
 ---> Running in d8132cda2e2a
Err:1 http://security.ubuntu.com/ubuntu zesty-security InRelease
  Cannot initiate the connection to security.ubuntu.com:80 (2001:67c:1560:8001::14). - connect (101: Network is unreachable) [IP: 2001:67c:1560:8001::14 80]
Err:2 http://archive.ubuntu.com/ubuntu zesty InRelease
  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8001::21). - connect (101: Network is unreachable) [IP: 2001:67c:1360:8001::21 80]
Err:3 http://archive.ubuntu.com/ubuntu zesty-updates InRelease
  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8001::21). - connect (101: Network is unreachable) [IP: 2001:67c:1360:8001::21 80]
Err:4 http://archive.ubuntu.com/ubuntu zesty-backports InRelease
  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8001::21). - connect (101: Network is unreachable) [IP: 2001:67c:1360:8001::21 80]
Reading package lists...
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/zesty/InRelease  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8001::21). - connect (101: Network is unreachable) [IP: 2001:67c:1360:8001::21 80]
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/zesty-updates/InRelease  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8001::21). - connect (101: Network is unreachable) [IP: 2001:67c:1360:8001::21 80]
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/zesty-backports/InRelease  Cannot initiate the connection to archive.ubuntu.com:80 (2001:67c:1360:8001::21). - connect (101: Network is unreachable) [IP: 2001:67c:1360:8001::21 80]
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/zesty-security/InRelease  Cannot initiate the connection to security.ubuntu.com:80 (2001:67c:1560:8001::14). - connect (101: Network is unreachable) [IP: 2001:67c:1560:8001::14 80]
W: Some index files failed to download. They have been ignored, or old ones used instead.
 ---> 6aa041d69aec
Removing intermediate container d8132cda2e2a
Step 3/12 : RUN apt-get install -y git curl apache2 php5 libapache2-mod-php5 php5-mcrypt php5-mysql
 ---> Running in 1b807c9fd593
Reading package lists...
Building dependency tree...
Reading state information...
E: Unable to locate package git
E: Unable to locate package curl
E: Unable to locate package apache2
E: Unable to locate package php5
E: Unable to locate package libapache2-mod-php5
E: Unable to locate package php5-mcrypt
E: Unable to locate package php5-mysql
The command '/bin/sh -c apt-get install -y git curl apache2 php5 libapache2-mod-php5 php5-mcrypt php5-mysql' returned a non-zero code: 100
Dockerbuild脚本运行时获得的/etc/resolve.conf的内容。它是系统生成的,来源于主机(我在主机上添加了google DNS)。Dockerfile脚本的输出确认了运行脚本的上下文中的内容:

Step 2/13 : RUN cat /etc/resolv.conf
 ---> Running in 68b494d1fcdb
; generated by /sbin/dhclient-script
search eu-west-1.compute.internal
nameserver 172.31.0.2
nameserver 8.8.8.8
在ec2实例上运行这个可以,但是我不能在Dockerfile脚本中运行它(因为ubuntu没有安装curl,并且由于网络问题,我不能运行apt get来安装任何东西):


发现了如何通过为docker指定一个不同的网络来使用/绑定它(一个名为“主机”的替代方案,与我认为的默认选项“网桥”不同)。我无法解释为什么一个工作,另一个不工作,因为我的网络知识不是很好

这篇文章促使我尝试它。它描述了docker创建的三个“默认网络”。调用docker时,可以选择要绑定到的特定网络。我认为“无”字面上意味着没有网络。我试过“桥牌”,出现了同样的症状。我尝试了“主机”并实现了互联网连接(apt获得更新),这回答了我的问题

列出docker可用网络的命令输出:

ec2 docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
4d37091ae99e        bridge              bridge              local
cab1b88f8084        host                host                local
592bd8b26a6a        none                null                local
在运行docker build时指定网络(关键是--network开关),apt get update的输出是更改生效的证据:

ec2 docker build --network host --no-cache -t moikaturns/amazon-ecs-sample .
Sending build context to Docker daemon 363.5 kB
Step 1/3 : FROM ubuntu:17.04
 ---> bde41be8de8c
Step 2/3 : RUN cat /etc/resolv.conf
 ---> Running in 62447e6613d6
; generated by /sbin/dhclient-script
search eu-west-1.compute.internal
nameserver 172.31.0.2
 ---> 7456dd87e8eb
Removing intermediate container 62447e6613d6
Step 3/3 : RUN apt-get update -y
 ---> Running in 19c5ba6e2a21
Get:1 http://security.ubuntu.com/ubuntu zesty-security InRelease [89.2 kB]
Get:2 http://archive.ubuntu.com/ubuntu zesty InRelease [243 kB]
Get:3 http://archive.ubuntu.com/ubuntu zesty-updates InRelease [89.2 kB]
...
Get:21 http://archive.ubuntu.com/ubuntu zesty-backports/main amd64 Packages [1438 B]
Fetched 24.2 MB in 3s (7702 kB/s)
Reading package lists...
 ---> bcf0a72ac69e
Removing intermediate container 19c5ba6e2a21
Successfully built bcf0a72ac69e
还发现以下内容很有用:

告诉docker启动一个交互式bash shell。可以通过交互方式观察网络困难,而不是运行Dockerfile脚本。令人沮丧的是,它是一个最小的ubuntu,并且像ping这样的基本工具没有安装。由于apt get install不起作用,因此可以安装它们以尝试进行大量调查

指定与docker run配合使用的网络可启用网络功能:

docker run --network=host -it ubuntu bash
感谢所有回答我问题的人

RCA:与aws跟进后,一位代表亲切地协助诊断出了什么问题(本教程在普通ec2实例上运行良好)。原因可追溯到我添加到ec2实例的iptables规则:

sudo /sbin/iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080

它将入站端口80重定向到内部端口8080。显然,它打乱了Docker(不知何故)创建的iptables条目。如果我删除此规则,则常规教程将在我的ec2实例上运行(iptables规则所做的全部工作是将端口80路由到8080,我可以使用代理(如nginx)来完成)。

我怀疑您的安全组未配置为允许此ec2实例与internet通信。IPv6也可能有问题。在ec2机器上,
cat/etc/resolv.conf
的输出是什么?另外,
curl ipinfo.io
在您的ec2机器上工作吗?我在ec2中遇到了与RHEL VM类似的问题。指定主机网络也不起作用!Yum install可在主机上工作,但不能在docker版本中工作。
docker run --network=host -it ubuntu bash
sudo /sbin/iptables -t nat -I PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 8080