全新的Ubuntu 14.04.2 Docker容器+ Nginx:连接在端口80上被拒绝

我遵循Digital Ocean的“如何在Ubuntu 14.04 LTS上安装Nginx”的说明 ,其中指出Nginx应该在安装后立即运行,但以下Dockerfile:

FROM ubuntu:14.04.2 RUN apt-get update -y RUN apt-get -y install curl RUN apt-get -y install nginx RUN curl http://127.0.0.1 | grep "Welcome to nginx!" 

给我这个错误:

 curl: (7) Failed to connect to 127.0.0.1 port 80: Connection refused 

要重现这一点:

  • 确保你的主机系统上安装了Docker
  • 将上面的代码插入到/whatever/path/Dockerfile
  • input“cd / whatever / path / && Docker build”。

这将使用Ubuntu构buildDocker容器,安装Nginx,然后尝试连接到127.0.0.1:80返回Nginx的欢迎页面。 这就是发生“连接被拒绝”错误的地方。

我的问题是:我怎么能从我的容器中调用“curl http://127.0.0.1并获得响应?

我的项目上的这个问题是https://github.com/dcycleproject/dcyclebox/issues/1

不要忘记首先启动nginx

RUN service nginx start && curl http://127.0.0.1 | grep "Welcome to nginx!"

关于数字海洋谈论操作系统与运行服务没有运行在docker集装箱(他们不应该)。 当你build立图像,你只需要安装必要的软件。 那么你应该根据这个图像运行容器 – 通常只有一个进程在容器中运行。 看官方的nginx图像例如 – https://github.com/dockerfile/nginx/blob/master/Dockerfile

在不同的RUN命令中运行apt-get update和apt-get install也是一个不好的做法 – https://docs.docker.com/articles/dockerfile_best-practices/#run