Search code examples
javaspringdockerdocker-swarm

Can't access any service's containers


I created an image: stavalfi/projecty:latest It is a very basic java spring application.

When I run a container without swarm, everything works fine:

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

working from chrome:

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

When I create a service using:

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

I can't ping my container and I can't reach my app from 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

I also created nginx:latest service by running:

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

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

The java spring boot controller code:

@RestController
public class HelloController {

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

Questions:

  1. How can I access my stavalfi/projecty:latest service from localhost:8080 and 10.0.2.15:8080 (the leader ip) ?

  2. How can I access ngrix service from localhost:8080 and not only from 10.0.2.15:8080 (the leader ip) ?


Solution

  • I used the following command to print the log of the running container (with stavalfi/projecty:latest image):

    docker logs <docker container name/id>
    

    I saw that my server can't find his hostname so he crushed for every HTTP GET request. Strangely,the container id doesn't add to the /etc/hosts file when I run a service but when I run a container, the containerID is added to the /etc/hosts file.

    I will love to know why if anyone know.