【问题标题】:docker: Says connection refused when attempting to connect to a published portdocker:尝试连接到已发布的端口时说连接被拒绝
【发布时间】:2016-05-11 23:03:15
【问题描述】:

我是 docker 的新手。我正在创建一个 Hello, World 示例。我要做的就是在 docker 中启动 Apache,然后从主机查看默认网站。

Dockerfile

FROM centos:latest

RUN yum install epel-release -y

RUN yum install wget -y

RUN yum install httpd -y

EXPOSE 80

ENTRYPOINT ["/usr/sbin/httpd", "-D", "FOREGROUND"]

然后我构建它: > docker build .

然后我标记它:

docker tag 17283f566320 my:apache

然后我运行它:

> docker run -p 80:9191 my:apache
AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.2. Set the 'ServerName' directive globally to suppress this message

然后运行....

在另一个终端窗口中,我尝试发出curl 命令来查看默认网站。

> curl -XGET http://0.0.0.0:9191
curl: (7) Failed to connect to 0.0.0.0 port 9191: Connection refused

> curl -XGET http://localhost:9191
curl: (7) Failed to connect to localhost port 9191: Connection refused

> curl -XGET http://127.0.0.1:9191
curl: (7) Failed to connect to 127.0.0.1 port 9191: Connection refused

或者我尝试本地主机

为了确保端口正确,我运行了这个:

> docker ps -l
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                          NAMES
5aed4063b1f6        my:apachep      "/usr/sbin/httpd -D F"   43 seconds ago      Up 42 seconds       80/tcp, 0.0.0.0:80->9191/tcp   angry_hodgkin

【问题讨论】:

  • 您的端口错误。应该是 9191:80。
  • 你的 apache 配置是怎么说的?监听的端口是什么。

标签: apache docker centos centos7


【解决方案1】:

谢谢大家。我的端口被颠倒了:

> docker run -p 9191:80 my:apache

【讨论】:

    【解决方案2】:

    如果您在 Ubuntu 机器上以本机方式运行 docker,您应该能够使用 localhost 访问您的容器。 如果您使用的是 Mac 或 Windows,您的 docker 容器不是在本地主机上运行,​​而是在其 IP 上运行。您可以使用命令 docker inspect <container id> | grep IPAddress 或者如果您使用的是 docker-machine docker-machine ip <docker_machine_name> 来获取您的容器 IP 相关信息:

    所以你的 curl 调用应该是这样的 curl <container_ip>:<container_exposed_port>

    您也可以使用参数-t 在构建命令上标记您的图像,如下所示: docker build -t my:image .

    另一个提示,您可以通过组合 yum install 命令来优化您的 dockerfile,如下所示:

    RUN yum install -y \ 
        epel-release \
        wget \
        httpd
    

    http://blog.tutum.co/2014/10/22/how-to-optimize-your-dockerfile/

    【讨论】:

      【解决方案3】:

      尽管您在本地计算机上创建了容器。这些实际上是在不同的机器(虚拟机)上运行的

      首先,检查你的 docker 机器(虚拟机)的 IP 是什么

      $docker-machine ls
      NAME      ACTIVE   DRIVER       STATE     URL                         SWARM
      default   *        virtualbox   Running   tcp://192.168.99.100  
      

      然后运行curl命令查看容器内你的apache web服务器上的默认网站

      curl http://192.168.99.100:9191
      

      【讨论】:

        猜你喜欢
        • 2021-11-16
        • 2019-10-20
        • 2016-02-15
        • 2021-06-24
        • 1970-01-01
        • 2016-12-24
        • 1970-01-01
        • 2019-09-06
        • 2021-01-20
        相关资源
        最近更新 更多