2017-10-28 94 views
0

我創建了一個圖像:stavalfi/projecty:latest 這是一個非常基本的java彈簧應用程序。無法訪問任何服務的容器

當我運行而不羣的容器中,一切工作正常:

docker run -d -p 8081:8080 --name container1 stavalfi/projecty:latest 

從鉻工作:

http://localhost:8081/ 
http://172.17.0.2:8080/ <<-- the address of the leader (no other nodes in the swarm) 

當我創建使用服務:

docker service create -p 8080:8080 --name service1 stavalfi/projecty:latest 

docker ps 
    CONTAINER ID  IMAGE      COMMAND     CREATED    STATUS    PORTS     NAMES 
    ab8cb85e9750  stavalfi/projecty:latest "/bin/sh -c '/usr/..." 5 minutes ago  Up 5 minutes  0.0.0.0:8081->8080/tcp container1 
    8928604253ed  stavalfi/projecty:latest "/bin/sh -c '/usr/..." 21 minutes ago  Up 21 minutes  8080/tcp     service1.1.uhpsxn9mke7fkfwpgwke8ugnt 
    e312e148de87  nginx:latest    "nginx -g 'daemon ..." 24 minutes ago  Up 24 minutes  80/tcp     web.1.zfihms3t4cy3h489srbfgrbw3 

我可以ping我的容器,我無法從Chrome訪問我的應用程序:

http://localhost:8080/ <<-- no answer 
http://10.0.2.15:8080/ <<-- response: 
Whitelabel Error Page 

This application has no explicit mapping for /error, so you are seeing this as a fallback. 

Sat Oct 28 18:24:26 UTC 2017 
There was an unexpected error (type=Internal Server Error, status=500). 
8928604253ed: 8928604253ed: Name or service not known 

http://10.0.2.15:8080/error <<-- response: 
Whitelabel Error Page 

This application has no explicit mapping for /error, so you are seeing this as a fallback. 

Sat Oct 28 18:25:34 UTC 2017 
There was an unexpected error (type=None, status=999). 
No message available 

我還創建nginx的:最新的服務運行:

docker service create -p 80:80 --name web nginx 

http://10.0.2.15:80/ <<-- working 
http://localhost:80/ <<-- not working 

的java的春天啓動控制器代碼:

@RestController 
public class HelloController { 

    @RequestMapping("/") 
    @SneakyThrows 
    public String index() { 
     String hostname = InetAddress.getLocalHost().getHostName(); 
     return "Hostname: "+hostname+ "! Greetings from Spring Boot!"; 
    } 
} 

問題:

  1. 我該如何訪問我的stavalfi/projecty:localhost:8080和10.0.2.15:8080(領導ip)的最新服務?

  2. 如何從本地主機:8080訪問ngrix服務,而不僅僅是從10.0.2.15:8080(首領ip)訪問?

回答

0

我用下面的命令打印運行容器的日誌(與stavalfi/projecty:最新的圖像):

docker logs <docker container name/id> 

我看到我的服務器無法找到自己的主機名,所以他爲每個HTTP GET請求壓縮。 奇怪的是,當我運行一個服務時,容器ID不會添加到/ etc/hosts文件,但是當我運行一個容器時,containerID被添加到/ etc/hosts文件中。

我很想知道爲什麼如果有人知道。