无法访问任何服务的容器

Can't access any service's containers

我创建了一个图像:stavalfi/projecty:latest 这是一个非常基本的 java spring 应用程序。

当我 运行 没有 swarm 的容器时,一切正常:

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

从 chrome 工作:

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

我还通过 运行ning 创建了 nginx:latest 服务:

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

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

javaspring引导控制器代码:

@RestController
public class HelloController {

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

问题:

  1. 如何从 localhost:8080 和 10.0.2.15:8080(领导 ip)访问我的 stavalfi/projecty:最新服务?

  2. 如何从 localhost:8080 而不仅仅是从 10.0.2.15:8080(领导 ip)访问 ngrix 服务?

我使用以下命令打印 运行ning 容器的日志(带有 stavalfi/projecty:最新图像):

docker logs <docker container name/id>

我看到我的服务器找不到他的主机名,所以他对每个 HTTP GET 请求都崩溃了。 奇怪的是,当我 运行 服务时,容器 ID 不会添加到 /etc/hosts 文件中,但是当我 运行 容器时,容器 ID 会添加到 /etc/hosts 文件中。

如果有人知道,我很想知道为什么。