如何从位于 Docker 容器内的应用程序发布 Web 服务?

huangapple go评论101阅读模式
英文:

How to publish a web service from an application inside a docker container?

问题

我有一个应用程序,它发布了一个Web服务,我尝试将其部署到Docker容器上,但它不起作用。
我使用了javax.jws中的@WebService和@WebMethod来声明我的服务,并使用以下方式发布它:

Endpoint.publish("http://localhost:8081/doctorservice",
                new DoctorServiceImplementation());

我的Dockerfile内容如下:

FROM openjdk:8
ADD target/service-publisher.jar service-publisher.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","service-publisher.jar"]

我使用以下命令创建了Docker镜像:

docker build -f Dockerfile -t webservice .

并使用以下命令运行它:

docker run --name webservice -p 8081:8081 -d webservice

容器正在运行并且端口已暴露,但当我尝试从浏览器访问http://localhost:8081/doctorservice?wsdl时,它不起作用。

英文:

I have an application which publishes a web service and I tried to deploy it on a docker container but it doesn't work.
I used @WebService and @WebMethod from javax.jws to declare my service and I published it with

Endpoint.publish("http://localhost:8081/doctorservice",
                new DoctorServiceImplementation());

The contents of my Dockerfile are

FROM openjdk:8
ADD target/service-publisher.jar service-publisher.jar
EXPOSE 8081
ENTRYPOINT ["java","-jar","service-publisher.jar"]

I created the docker image with

docker build -f Dockerfile -t webservice .

And run it with

docker run --name webservice -p 8081:8081 -d webservice 

The container runs and the ports are exposed but when I try to access http://localhost:8081/doctorservice?wsdl from the browser it doesn't work.

答案1

得分: 2

我找到了解决我的问题的方法:我必须将服务发布到0.0.0.0而不是localhost,所以我替换了

Endpoint.publish("http://localhost:8081/doctorservice",
new DoctorServiceImplementation());

Endpoint.publish("http://0.0.0.0:8081/doctorservice",
new DoctorServiceImplementation());

用于在Docker容器内运行的应用程序。

英文:

I found the solution to my problem: I had to publish the service to 0.0.0.0 instead of localhost so I replaced

Endpoint.publish("http://localhost:8081/doctorservice",
                new DoctorServiceImplementation());

with

Endpoint.publish("http://0.0.0.0:8081/doctorservice",
                new DoctorServiceImplementation());

for the app running inside the docker container

答案2

得分: -1

一开始看,除了你试图访问的地址之外,你做得都正确。
即使服务已公开,你也不在容器的“localhost”上,因此你应该使用容器的IP地址。

简而言之,不要使用http://localhost:8081/doctorservice?wsdl,而是尝试这个http://<容器IP地址>:8081/doctorservice?wsdl。

查看这个答案以获取容器的IP地址:

https://stackoverflow.com/questions/17157721/how-to-get-a-docker-containers-ip-address-from-the-host

英文:

At first glance, you did everything correct except for the address you are trying to reach.
Even if the service is exposed you are not in the "localhost" of the container, hence you should use the ip of the container.

TLDR, instead of http://localhost:8081/doctorservice?wsdl try this http://<CONTAINER_IP_ADDRESS>:8081/doctorservice?wsdl

Check this answer to fetch the IP address of your container:

https://stackoverflow.com/questions/17157721/how-to-get-a-docker-containers-ip-address-from-the-host

huangapple
  • 本文由 发表于 2020年1月3日 22:16:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/59580106.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定