Connect() failed (111: Connection refused) while connecting to upstream

Hi,
I am using Kong and spring boot applications in Docker
Spring boot app configuration:

server.servlet.context-path=/userservice
server.port=9090
spring.application.name=userservice

Here is the Dockerfile

FROM openjdk:12
ADD target/userservice.jar userservice.jar
EXPOSE 9090
ENTRYPOINT [“java”,“-jar”,“userservice.jar”]

Docker command to push microservice to docker

docker run -p 9090:9090 userservice

APIs added to kong

curl -i -X POST
–url http://localhost:8001/services/
–data ‘name=userservice’
–data ‘url=http://localhost:9090/userservice

curl -i -X POST
–url http://localhost:8001/services/userservice/routes
–data ‘hosts=api.com

curl -i -X GET
–url http://localhost:8000/
–header ‘Host: api.com

And the outcome is

*2019/06/12 13:31:13 [error] 39#0: 184849 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: kong, request: “GET / HTTP/1.1”, upstream: “http://127.0.0.1:9090/userservice”, host: “api.com

Any clues?
-Rana

Docker should be usually running on host ip and not loopback address (127…0.0.1) so replace “localhost” in your service url in your services config to http://:9090/…

Thanks mgkong for the response.
That has to be done at nginx configuration inside the docker. right?
Can you please give more insight?

In service definition of your Kong server “userservice”, I see the service URL configured is http://localhost:9090/userservice. In this service URL, try replacing “localhost” with actual IP address of the host where this upstream services (hosted on docker) is running.

thanks mgkong. i tried the both public ip and local network ip without any success

when i do docker inspect on my spring boot container below o/p observed

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

i logged in to KONG container and curl the above ip’s, i see one 302 and another one connection refused

/ # curl -i -H “Accept: application/json” -H “Content-Type: application/json” -X GET http://172.18.0.1:9090/userservice

**HTTP/1.1 302 **

Location : http://172.18.0.1:9090/userservice/

Transfer-Encoding : chunked

Date : Sat, 15 Jun 2019 03:14:07 GMT

and when i do the same on 172.18.0.2

/ # 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

but i can see from my host machine browser

http://localhost:9090/userservice/ is working fine

any thing i miss here

i think the network ip is different in diff networks here


"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
1 Like

Thank you! It took me 3 days of searching to find this solution.
I am using docker compose for 2 services + kong gateway. I commented out the ports for the services in the docker compose file and I am no longer getting Connection refused.