当我在dockerfile中公开端口并成功构建但在服务器中没有响应时

时间:2019-06-14 11:50:10

标签: docker

当我运行此命令时 docker build -t my_image

我将其作为输出

Sending build context to Docker daemon  2.048kB
Step 1/5 : FROM php:7.2-apache
 ---> f046c4ead123
Step 2/5 : MAINTAINER JLT 7 <j*******t.******@gmail.com>
 ---> Using cache
 ---> 2d04942bf1f3
Step 3/5 : RUN apt-get update
 ---> Using cache
 ---> 8f5c190e13ab
Step 4/5 : CMD ["echo","Hello World!!!...My frst message"]
 ---> Using cache
 ---> 9d67b4f4cf85
Step 5/5 : EXPOSE 8090
 ---> Using cache
 ---> 7ad8354944b2
Successfully built 7ad8354944b2
Successfully tagged my_image:latest
SECURITY WARNING: You are building a Docker image from Windows against a non-Windows Docker host. All files and directories added to build context will have '-rwxr-xr-x' permissions. It is recommended to double check and reset permissions for sensitive files and directories.

但是当我在服务器上运行此端口时,它没有响应...

1 个答案:

答案 0 :(得分:1)

EXPOSE Dockerfile指令本身不会发布容器的端口(请参阅Dockerfile reference section in the documentation;该指令仅将元数据添加到您构建的映像中,并允许您指示容器的哪些端口。容器中运行的服务会监听。

EXPOSE指令是可选,即使不设置该端口,您也可以连接到容器正在侦听的端口。

为了使端口可访问;

  • 首先,确保容器中的服务在端口上侦听(您的示例Dockerfile基于官方的php:7.2-apache映像,listens on port 80 by default
  • 确保容器中的服务正在侦听任何IP地址(0.0.0.0),而不是localhost / 127.0.0.1(因为本地主机是容器< / em>,这意味着如果您的服务在localhost上侦听,则只能从容器本身内部进行访问)
  • 运行容器时,可以将容器的端口映射到希望其访问的端口(“端口映射”)(另请参见文档中的networking section。< / li>

-p / --publish选项具有简写形式和高级语法;我将在示例中使用速记语法。该表示法使用的格式为:-p <host-port>:<container-port>

例如,如果您的容器正在侦听端口80,并且您想将该端口发布(或“映射”)到主机上的端口8090

docker run -d -p 8090:80 myimage

您现在可以在以下位置到达您的容器:

  • http://<ip-address of your host>:8090
  • (取决于您的设置):http://localhost:8090