【问题标题】:Brand new Ubuntu 14.04.2 Docker container + Nginx: connection refused on port 80全新的 Ubuntu 14.04.2 Docker 容器 + Nginx:端口 80 连接被拒绝
【发布时间】:2015-05-21 23:04:08
【问题描述】:

我按照Digital Ocean's "How To Install Nginx on Ubuntu 14.04 LTS" 的说明进行操作,其中指出 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
  • 输入 'cd /whatever/path/ && Docker build.'

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

我的问题是:如何从容器中调用“curl http://127.0.0.1”并获得响应?

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

【问题讨论】:

  • 已经绑定到该端口的其他东西(例如 Apache)?
  • 正如@marekful 建议的那样,您可能需要检查端口是否正在使用中。尝试运行netstat -an | grep :80 | grep LISTEN
  • @Jpatrick,它是一个 docker 镜像创建,那里没有运行任何东西
  • @ISanych。好点子。

标签: ubuntu nginx docker


【解决方案1】:

别忘了先开始nginx

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

【讨论】:

  • 谢谢!有趣的是,让 RUN service nginx start 和 RUN curl localhost | grep "欢迎使用 nginx!"在单独的行上会导致端口 80 连接被拒绝问题,但使用 && 在同一行上运行它们可以工作!
【解决方案2】:

关于数字海洋的说明,谈论运行服务的操作系统,这些服务不在 docker 容器中运行(它们不应该运行)。构建映像时,您只需安装必要的软件。然后你应该基于这个镜像运行容器——通常只有一个进程在容器中运行。以官方 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

【讨论】:

    猜你喜欢
    • 2019-09-06
    • 2021-06-19
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 1970-01-01
    相关资源
    最近更新 更多