Docker Community Forums

Share and learn in the Docker community.

Container to Container comunication

Hi , i deployed API gate way KONG and a simple spring boot micro service on my docker instance(MAC docker installation)

i exposed port of spring boot application and i can access it from my host machine(via browser)

However when i try via KONG instance, it gives 502 error
below is the command to install kong

docker run -d --name kong-database \
               --network=kong-net \
               -p 9042:9042 \
               cassandra:3

docker run --rm \
     --network=kong-net \
     -e "KONG_DATABASE=cassandra" \
     -e "KONG_PG_HOST=kong-database" \
     -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
     kong:latest kong migrations bootstrap

docker run -d --name kong \
     --network=kong-net \
     -e "KONG_DATABASE=cassandra" \
     -e "KONG_PG_HOST=kong-database" \
     -e "KONG_CASSANDRA_CONTACT_POINTS=kong-database" \
     -e "KONG_PROXY_ACCESS_LOG=/dev/stdout" \
     -e "KONG_ADMIN_ACCESS_LOG=/dev/stdout" \
     -e "KONG_PROXY_ERROR_LOG=/dev/stderr" \
     -e "KONG_ADMIN_ERROR_LOG=/dev/stderr" \
     -e "KONG_ADMIN_LISTEN=0.0.0.0:8001, 0.0.0.0:8444 ssl" \
     -p 8000:8000 \
     -p 8443:8443 \
     -p 8001:8001 \
     -p 8444:8444 \
     kong:latest

used below commands for building posting micro service on kong
Dockerfile
FROM openjdk:12
ADD target/userservice.jar userservice.jar
EXPOSE 9090
ENTRYPOINT [“java”,"-jar",“userservice.jar”]

docker build -f Dockerfile -t userservice .
docker run -p 9090:9090 --network=kong-net --hostname=userservice userservice

i logged in to userservice container and identified the IP

“Gateway”: “172.17.0.1”,
“IPAddress”: “172.17.0.2”,
“IPPrefixLen”: 16,
“IPv6Gateway”: “”

when i logged in to kong container and curl its refusing connection

/ # curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://172.18.0.2:9090/userservice

curl: (7) Failed to connect to 172.18.0.2 port 9090: Connection refused

i wonder how i can access microservice from out side of docker world(http://localhost:9090/userservice/)
and unable to access from the other container of the same docker.
i goal is to reach the microservice via gateway(kong here). i added necessary services and routes to kong and found 502 issue(upstream url not found), then i am trying to accessing the services with in docker containers as explained above

any thing i miss here?
-Rana

and my network settings are


"NetworkSettings": {
            "Bridge": "",
            "SandboxID": "dbadc3aed953f3b08666c4ae43164a5d33740300f2b58a938ae8f60f1c556402",
            "HairpinMode": false,
            "LinkLocalIPv6Address": "",
            "LinkLocalIPv6PrefixLen": 0,
            "Ports": {
                "9090/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "9090"
                    }
                ]
            },
            "SandboxKey": "/var/run/docker/netns/dbadc3aed953",
            "SecondaryIPAddresses": null,
            "SecondaryIPv6Addresses": null,
            "EndpointID": "",
            "Gateway": "",
            "GlobalIPv6Address": "",
            "GlobalIPv6PrefixLen": 0,
            "IPAddress": "",
            "IPPrefixLen": 0,
            "IPv6Gateway": "",
            "MacAddress": "",
            "Networks": {
                "kong-net": {
                    "IPAMConfig": null,
                    "Links": null,
                    "Aliases": [
                        "524b0361c5c6"
                    ],
                    "NetworkID": "fad502a41154a6422a3270a859eae6c048316fb959ad54d664ad37b49051fa47",
                    "EndpointID": "6b35cf7d1535db925a720013f0b647695e1cde7c2a257b14a12972107c216858",
                    "Gateway": "172.18.0.1",
                    "IPAddress": "172.18.0.4",
                    "IPPrefixLen": 16,
                    "IPv6Gateway": "",
                    "GlobalIPv6Address": "",
                    "GlobalIPv6PrefixLen": 0,
                    "MacAddress": "02:42:ac:12:00:04",
                    "DriverOpts": null
                }
            }
        }

i managed to solve the issue. the change worked here is i removed exposure the Spring boot app to the outside world
Changed
docker run -p 9090:9090 userservice

to
docker run -d --name userservice
–net kong-net
userservice