I'm trying to run an embedded undertow server inside a docker container. When I run the following code snippet in my machine, I can able to hit the http endpoint which returns "Hello World". But I can't reach the endpoint when I run the same code inside the docker container.
public class HelloWorldServer {
public static void main(final String[] args) {
Undertow server = Undertow.builder()
.addHttpListener(8080, "localhost")
.setHandler(new HttpHandler() {
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/plain");
exchange.getResponseSender().send("Hello World");
}
}).build();
server.start();
}
}
I found the above example from the following link http://undertow.io/undertow-docs/undertow-docs-1.4.0/index.html
These are all the following commands I execute to build and run the container.
docker build -t z .
docker run -d -p 8080:8080 -t z
The undertow server failed to bind http listener with my machine's IP. instead of specifying
localhost
I changed to
InetAddress.getLocalHost().getHostAddress()
which bind the listener with my machine's IP. Now I can able to hit the endpoint.