来自Dockerfile的HTTP请求不成功

我正在玩Docker,并试图让Dockerfile运行ubuntu和nginx。

“docker build”的结果是curl无法在localhost上执行HTTP请求,但是如果我稍后启动从Dockerfile创build的容器,它工作得很好..

这里可能是什么问题?

请参阅下面的Dockerfile:

$ cat Dockerfile FROM ubuntu:14.10 RUN apt-get update RUN apt-get install -y curl nginx RUN service nginx start RUN echo "niklas9 was here" > /usr/share/nginx/html/index.html RUN /usr/bin/curl -v "http://localhost/" 

来自“docker build”的结果:

 $ sudo docker.io build . ... Step 5 : RUN /usr/bin/curl -v "http://localhost/" ---> Running in 46f773be22a2 * Hostname was NOT found in DNS cache % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying ::1... * connect to ::1 port 80 failed: Connection refused * Trying 127.0.0.1... * connect to 127.0.0.1 port 80 failed: Connection refused * Failed to connect to localhost port 80: Connection refused * Closing connection 0 curl: (7) Failed to connect to localhost port 80: Connection refused 2014/11/26 22:47:38 The command [/bin/sh -c /usr/bin/curl -v "http://localhost/"] returned a non-zero code: 7 

启动容器并附加到它的结果:

 root@65c55d5974cb:/# curl -v "http://localhost/" * Hostname was NOT found in DNS cache * Trying ::1... * Connected to localhost (::1) port 80 (#0) > GET / HTTP/1.1 > User-Agent: curl/7.37.1 > Host: localhost > Accept: */* > < HTTP/1.1 200 OK * Server nginx/1.6.2 (Ubuntu) is not blacklisted < Server: nginx/1.6.2 (Ubuntu) < Date: Wed, 26 Nov 2014 21:50:16 GMT < Content-Type: text/html < Content-Length: 17 < Last-Modified: Wed, 26 Nov 2014 21:38:11 GMT < Connection: keep-alive < ETag: "54764843-11" < Accept-Ranges: bytes < niklas9 was here * Connection #0 to host localhost left intact 

我使用apt-get安装了docker,运行Ubuntu 14,请参阅下面的版本。

 $ docker.io --version Docker version 0.9.1, build 3600720 

从根本上说,你不应该想到在build造时间开始你的服务。

Dockerfile RUN命令旨在为您正在尝试创build的最终容器创build一些状态。 每个命令创build一个新的容器层,基于前一个,Dockercaching它们以加快速度,所以任何给定的RUN命令实际上都可能不会为一个构build运行,除非它之前的事情已经改变了。

从Docker的一些笔记如何工作

执行RUN服务nginx启动之后,只有文件系统中的更改是持久的。 dockerfile的下一行执行时,nginx进程不可用。

它会这样工作,但是如果你想在容器中启动nginx进程,你需要在最后添加一个CMD

 FROM ubuntu:14.10 RUN apt-get update RUN apt-get install -y curl nginx RUN echo "niklas9 was here" > /usr/share/nginx/html/index.html RUN service nginx start && /usr/bin/curl -v "http://localhost/"