简体   繁体   English

docker-compose:将docker镜像的端口暴露给主机

[英]docker-compose: Exposing port of docker image to host

I have this docker-compose.yml snippet which is intended to expose a SOAP endpoint in Java built by Maven to other docker images (not included in snippet, but they work): 我有这个docker-compose.yml片段,该片段旨在将由Maven构建的Java中的SOAP端点公开给其他docker图像(不包含在片段中,但它们可以工作):

mocksumma: image: openjdk:9-jdk ports: - "56808:56808" expose: [56808] volumes: - ./mocksumma/target/mocksumma-1.0-SNAPSHOT.jar:/mocksumma.jar #command: "sleep 10000000000" command: "java --add-modules java.se.ee -jar /mocksumma.jar 'http://localhost:56808/mediehub/search/services/SearchWS?wsdl'"

I would also like to access this from my host environment (Ubuntu 17.04, docker compose 1.8.1, docker 1.12.6), but when accessing http://localhost:56808 I get either connection closed or ERR_SOCKET_NOT_CONNECTED. 我也想从我的主机环境(Ubuntu 17.04,docker compose 1.8.1,docker 1.12.6)中访问它,但是当访问http:// localhost:56808时,我关闭了连接或ERR_SOCKET_NOT_CONNECTED。 The web service has been confirmed to be responsive using "telnet localhost 56808" inside the container itself). 已使用容器本身内的“ telnet localhost 56808”确认该Web服务响应。 Telnet from the host immediately returns connection closed: 主机上的Telnet立即返回关闭的连接:

$ telnet localhost 56808 Trying 127.0.0.1... Connected to localhost. Escape character is '^]'. Connection closed by foreign host.

My theory is that for some reason the port is not exposed or forwarded (ports) to the host, because I have either overlooked or misunderstood something. 我的理论是,由于某种原因,端口没有暴露或转发(端口)给主机,因为我忽略或误解了某些内容。

Suggestions? 有什么建议吗?

You should change your command to 您应该将命令更改为

command: "java --add-modules java.se.ee -jar /mocksumma.jar 'http://0.0.0.0:56808/mediehub/search/services/SearchWS?wsdl'"

So the updated compose file will be 因此,更新后的撰写文件将是

 mocksumma:
    image: openjdk:9-jdk
    ports:
    - "56808:56808"
    expose: [56808]
    volumes:
    - ./mocksumma/target/mocksumma-1.0-SNAPSHOT.jar:/mocksumma.jar
    #command: "sleep 10000000000"
    command: "java --add-modules java.se.ee -jar /mocksumma.jar 'http://0.0.0.0:56808/mediehub/search/services/SearchWS?wsdl'"

Looking at the command which worked inside container, I assumed that your mocksumma.jar file binds to network interfaces based on the argument url. 查看在容器内部起作用的命令,我假设您的mocksumma.jar文件基于参数url绑定到网络接口。 This means when you use use url as http://0.0.0.0:56808/mediehub/search/services/SearchWS?wsdl it binds to localhost:56808 . 这意味着当您使用使用URL作为http://0.0.0.0:56808/mediehub/search/services/SearchWS?wsdl它将绑定到localhost:56808

Now inside a container localhost will point to the container's loopback interface and it will only answer when the traffic comes from inside the container itself. 现在,在容器内部, localhost将指向容器的环回接口,并且仅在流量来自容器内部时才应答。 When you try to map this port or another container tries to reach this container at 56808 the request is coming on eth0 or similar interface of the container. 当您尝试映射此端口或另一个容器尝试在56808到达此容器时,请求来自eth0或该容器的类似接口。

But your bind is only to localhost:56808 . 但是您的绑定仅绑定到localhost:56808 To solve the issue you need to bind it to all available interfaces which can be done by binding to 0.0.0.0:56808 . 要解决此问题,您需要将其绑定到所有可用接口,这可以通过绑定到0.0.0.0:56808来完成。 0.0.0.0 is a special IPv4 address which is used for binding to all available interfaces 0.0.0.0是一个特殊的IPv4地址,用于绑定到所有可用接口

Try using this format, like the documentation 尝试使用这种格式,例如文档

https://docs.docker.com/compose/compose-file/#expose https://docs.docker.com/compose/compose-file/#expose

Example

 mocksumma:
image: openjdk:9-jdk
ports:
- "56808:56808"
expose: 
- "56808"
volumes:
- ./mocksumma/target/mocksumma-1.0-SNAPSHOT.jar:/mocksumma.jar
#command: "sleep 10000000000"
command: "java --add-modules java.se.ee -jar /mocksumma.jar 'http://localhost:56808/mediehub/search/services/SearchWS?wsdl'"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM