简体   繁体   中英

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):

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. The web service has been confirmed to be responsive using "telnet localhost 56808" inside the container itself). Telnet from the host immediately returns connection closed:

$ 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. This means when you use use url as http://0.0.0.0:56808/mediehub/search/services/SearchWS?wsdl it binds to 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. 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.

But your bind is only to 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 is a special IPv4 address which is used for binding to all available interfaces

Try using this format, like the documentation

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'"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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