一千萬個為什麽

搜索

Java ReST服務在docker上運行但無法訪問



我是Docker的新手

Docker版本18.03.0-ce,在RHEL 7上運行的構建0520e24。

我使用 Spring教程創建了一個簡單的ReST服務。超級jar在我的Windows機器上運行良好,我可以通過瀏覽器訪問該服務。

Dockerfile:

# fetch basic image
FROM openjdk:latest

# application placed into /opt/app
RUN mkdir -p /opt/app
WORKDIR /opt/app

# local application port
EXPOSE 8080

現在,我執行了以下步驟

1.建立圖像

docker build --tag=java-rest

2.運行容器

docker run -p 18080:8080 -t -i java-rest

3.檢查容器是否已啟動

[[email protected] ~]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS                          PORTS                     NAMES
38ec56c99135        java-rest           "bash"              About a minute ago   Up About a minute               0.0.0.0:18080->8080/tcp   infallible_yalow

4.將jar文件復制到容器中

docker cp /localhome/ojoqcu/code/java/gs-rest-service-0.1.0.jar 38ec56c99135:/opt/app/gs-rest-service-0.1.0.jar

5.運行容器和罐子

docker run -p 18080:8080 -t -i java-rest
[email protected]:/opt/app# ls
gs-rest-service-0.1.0.jar
[email protected]:/opt/app#
[email protected]:/opt/app#
[email protected]:/opt/app# java -jar gs-rest-service-0.1.0.jar

6.在容器上,應用程序正在運行

[email protected]:/opt/app# ps -eaf
UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 13:43 pts/0    00:00:00 bash
root         8     1  0 13:47 pts/0    00:00:14 java -jar gs-rest-service-0.1.0.
root        54     0  0 14:16 pts/1    00:00:00 bash
root        60    54  0 14:17 pts/1    00:00:00 ps -eaf

答復:

1.從同一臺機器(RHEL 7)

[[email protected] ~]# curl http://localhost:18080
<html>
<head><title>Redirection</title></head>
<body>

Redirect

</body>
[[email protected] ~]#



[[email protected] ~]# curl http://0.0.0.0:18080
<html><head>
<title>Network Error</title>
</head>
<body>


<table border=0 cellPadding=1 width="80%"> <tr><td> Network Error (tcp_error)

</td></tr> <tr><td> A communication error occurred: "" </td></tr> <tr><td> The Web Server may be down, too busy, or experiencing other problems preventing it from responding to requests. You may wish to try again at a later time. </td></tr> <tr><td>
For assistance, contact your network support team.
</td></tr> </table>
 

</body></html>
[[email protected] ~]#



[[email protected] ~]# curl http://l5341t:18080
<html><head>
<title>Network Error</title>
</head>
<body>


<table border=0 cellPadding=1 width="80%"> <tr><td> Network Error (dns_unresolved_hostname)

</td></tr> <tr><td> Your requested host "l5341t" could not be resolved by DNS. </td></tr> <tr><td> </td></tr> <tr><td>
For assistance, contact your network support team.
</td></tr> </table>
 

</body></html>
[[email protected] ~]#
[[email protected] ~]#

2.從我當地的Windows機器的瀏覽器(我認為它仍然是Spring Boot的嵌入式服務器,但不確定!)

http://l5341t:18080/
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Thu May 24 14:13:44 UTC 2018
There was an unexpected error (type=Not Found, status=404).
No message available

轉載註明原文: Java ReST服務在docker上運行但無法訪問

一共有 1 個回答:

我沒有深入挖掘Spring代碼(它在WIndows/Linux機器上完美運行,但不在容器中運行!)但似乎問題實際上是代碼而不是Docker配置。我在這裏找到了答案。