我正在与Docker一起玩,并试图让Dockerfile在运行ubuntu和nginx时工作。
“docker build”的结果是curl无法在本地主机上执行HTTP请求,但是,如果我以后启动从Dockerfile创建的容器,它就可以正常工作。
这里可能是什么问题?
请参阅下面的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
我正在运行Ubuntu 14,并且docker安装了apt-get,请参见下面的版本。
$ docker.io --version
Docker version 0.9.1, build 3600720
最佳答案
从根本上讲,您不应该考虑在构建时启动服务。
Dockerfile RUN
命令旨在为您尝试创建的最终容器创建某种状态。每个命令都基于之前的命令创建一个新的容器层,并且Docker对其进行缓存以加快处理速度,因此任何给定的RUN
命令实际上可能根本不会在构建中运行,除非之前发生的事情已更改。
Some notes from Docker on how this works
关于docker - 来自Dockerfile的HTTP请求不成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27159522/