I have this simple Java SOAP service which works fine in the local machine.
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class EchoService {
@WebMethod
public String echo(@WebParam(name = "message") String message) {
return message;
}
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/echoservice", new EchoService());
System.out.println("Service published at http://localhost:8080/echoservice");
}
}
When I start this locally, I can reach the service via http://localhost:8080/echoservice
Then I tried to use docker and deploy a containerised version. This is my Dockerfile
FROM maven:3.8.4-openjdk-11 AS build
WORKDIR /app
COPY pom.xml .
RUN mvn dependency:go-offline
COPY src ./src
RUN mvn package
FROM openjdk:11-jre-slim
WORKDIR /app
COPY --from=build /app/target/*.jar ./app.jar
COPY --from=build /app/target/libs ./libs
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "app.jar"]
These are my docker commands.
docker build -t java-soap:1.0 .
docker run -p 8080:8080 java-soap:1.0
After starting the container, I can see the server is running but it is no longer reachable via http://localhost:8080/echoservice.
What should be the issue here? Is this related to Docker network interfaces? Is there a way to fix this?
This worked fine when I changed localhost
to 0.0.0.0
.
Corrected code,
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class EchoService {
@WebMethod
public String echo(@WebParam(name = "message") String message) {
return message;
}
public static void main(String[] args) {
Endpoint.publish("http://0.0.0.0:8080/echoservice", new EchoService());
System.out.println("Service published at http://localhost:8080/echoservice");
}
}