docker:尝试连接到已发布的端口时表示连接被拒绝
docker: Says connection refused when attempting to connect to a published port
我是 docker 的新手。我正在创建一个 Hello, World 示例。我要做的就是在 docker 中启动 Apache,然后从主机查看默认网站。
Docker 文件
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
然后 运行s....
在另一个终端 window 中,我尝试发出 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
或者我试试 localhost
为了确保端口正确,我运行这样做:
> 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
如果您在 Ubuntu 机器上 运行 docker 作为本机,您应该能够使用本地主机访问您的容器。
如果您使用 Mac 或 Windows,您的 docker 容器不是在本地主机上运行,而是在其 IP 上运行。你可以使用命令 docker inspect <container id> | grep IPAddress
或者如果你使用的是 docker-machine docker-machine ip <docker_machine_name>
来获取你的容器 ip
相关信息:
- http://networkstatic.net/10-examples-of-how-to-get-docker-container-ip-address/
- https://docs.docker.com/machine/reference/ip/
- How to get a Docker container's IP address from the host?
所以你的 curl 调用应该是这样的 curl <container_ip>:<container_exposed_port>
您也可以在构建命令中使用参数 -t
标记图像,如下所示:
docker build -t my:image .
另一个提示,你可以通过像这样组合 yum 安装命令来优化你的 docker 文件:
RUN yum install -y \
epel-release \
wget \
httpd
http://blog.tutum.co/2014/10/22/how-to-optimize-your-dockerfile/
尽管您在本地机器上创建了容器。这些实际上 运行ning 在不同的机器(虚拟机)
首先,检查您的 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
感谢大家。我的端口被颠倒了:
> docker run -p 9191:80 my:apache
我是 docker 的新手。我正在创建一个 Hello, World 示例。我要做的就是在 docker 中启动 Apache,然后从主机查看默认网站。
Docker 文件
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
然后 运行s....
在另一个终端 window 中,我尝试发出 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
或者我试试 localhost
为了确保端口正确,我运行这样做:
> 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
如果您在 Ubuntu 机器上 运行 docker 作为本机,您应该能够使用本地主机访问您的容器。
如果您使用 Mac 或 Windows,您的 docker 容器不是在本地主机上运行,而是在其 IP 上运行。你可以使用命令 docker inspect <container id> | grep IPAddress
或者如果你使用的是 docker-machine docker-machine ip <docker_machine_name>
来获取你的容器 ip
相关信息:
- http://networkstatic.net/10-examples-of-how-to-get-docker-container-ip-address/
- https://docs.docker.com/machine/reference/ip/
- How to get a Docker container's IP address from the host?
所以你的 curl 调用应该是这样的 curl <container_ip>:<container_exposed_port>
您也可以在构建命令中使用参数 -t
标记图像,如下所示:
docker build -t my:image .
另一个提示,你可以通过像这样组合 yum 安装命令来优化你的 docker 文件:
RUN yum install -y \
epel-release \
wget \
httpd
http://blog.tutum.co/2014/10/22/how-to-optimize-your-dockerfile/
尽管您在本地机器上创建了容器。这些实际上 运行ning 在不同的机器(虚拟机)
首先,检查您的 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
感谢大家。我的端口被颠倒了:
> docker run -p 9191:80 my:apache