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

**URL:** <https://discuss.konghq.com/t/connect-failed-111-connection-refused-while-connecting-to-upstream/3731>\
**Category:** General\
**Tags:** kong-gateway\
**Created:** [June 12, 2019, 1:37pm UTC](https://discuss.konghq.com/t/connect-failed-111-connection-refused-while-connecting-to-upstream/3731 "2019-06-12T13:37:42Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![ranadheer\_machineni](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/ranadheer_machineni/32/1105_2.png) [@ranadheer\_machineni](https://discuss.konghq.com/u/ranadheer_machineni)\
**Post date:** [June 12, 2019, 1:37pm UTC](https://discuss.konghq.com/t/connect-failed-111-connection-refused-while-connecting-to-upstream/3731/1 "2019-06-12T13:37:42Z")

</div>

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/](http://localhost:8001/services/)   
–data ‘name=userservice’   
–data ‘url=[http://localhost:9090/userservice](http://localhost:9090/userservice)’

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

curl -i -X GET   
–url [http://localhost:8000/](http://localhost:8000/)   
–header ‘Host: [api.com](http://api.com)’

---

<div class="post-metadata">

**Author:** ![ranadheer\_machineni](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/ranadheer_machineni/32/1105_2.png) [@ranadheer\_machineni](https://discuss.konghq.com/u/ranadheer_machineni)\
**Post date:** [June 12, 2019, 1:38pm UTC](https://discuss.konghq.com/t/connect-failed-111-connection-refused-while-connecting-to-upstream/3731/2 "2019-06-12T13:38:46Z")

</div>

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](http://127.0.0.1:9090/userservice)”, host: “[api.com](http://api.com)”_

Any clues?  
-Rana

---

<div class="post-metadata">

**Author:** ![mgkong](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@mgkong](https://discuss.konghq.com/u/mgkong)\
**Post date:** [June 12, 2019, 5:13pm UTC](https://discuss.konghq.com/t/connect-failed-111-connection-refused-while-connecting-to-upstream/3731/3 "2019-06-12T17:13:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ranadheer\_machineni](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/ranadheer_machineni/32/1105_2.png) [@ranadheer\_machineni](https://discuss.konghq.com/u/ranadheer_machineni)\
**Post date:** [June 13, 2019, 1:36am UTC](https://discuss.konghq.com/t/connect-failed-111-connection-refused-while-connecting-to-upstream/3731/4 "2019-06-13T01:36:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mgkong](https://avatars.discourse-cdn.com/v4/letter/m/ce7236/32.png) [@mgkong](https://discuss.konghq.com/u/mgkong)\
**Post date:** [June 13, 2019, 3:55pm UTC](https://discuss.konghq.com/t/connect-failed-111-connection-refused-while-connecting-to-upstream/3731/5 "2019-06-13T15:55:29Z")

</div>

In service definition of your Kong server “userservice”, I see the service URL configured is [http://localhost:9090/userservice](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.

---

<div class="post-metadata">

**Author:** ![ranadheer\_machineni](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/ranadheer_machineni/32/1105_2.png) [@ranadheer\_machineni](https://discuss.konghq.com/u/ranadheer_machineni)\
**Post date:** [June 14, 2019, 2:07am UTC](https://discuss.konghq.com/t/connect-failed-111-connection-refused-while-connecting-to-upstream/3731/6 "2019-06-14T02:07:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ranadheer\_machineni](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/ranadheer_machineni/32/1105_2.png) [@ranadheer\_machineni](https://discuss.konghq.com/u/ranadheer_machineni)\
**Post date:** [June 15, 2019, 3:18am UTC](https://discuss.konghq.com/t/connect-failed-111-connection-refused-while-connecting-to-upstream/3731/7 "2019-06-15T03:18:54Z")

</div>

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://172.18.0.1:9090/userservice)**

\*\*HTTP/1.1 302 \*\*

**Location : [http://172.18.0.1:9090/userservice/](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](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/](http://localhost:9090/userservice/) is working fine

any thing i miss here

---

<div class="post-metadata">

**Author:** ![ranadheer\_machineni](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/ranadheer_machineni/32/1105_2.png) [@ranadheer\_machineni](https://discuss.konghq.com/u/ranadheer_machineni)\
**Post date:** [June 15, 2019, 4:59am UTC](https://discuss.konghq.com/t/connect-failed-111-connection-refused-while-connecting-to-upstream/3731/8 "2019-06-15T04:59:28Z")

</div>

```
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
                }
            }
        }
```

---

<div class="post-metadata">

**Author:** ![ranadheer\_machineni](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/ranadheer_machineni/32/1105_2.png) [@ranadheer\_machineni](https://discuss.konghq.com/u/ranadheer_machineni)\
**Post date:** [June 15, 2019, 2:29pm UTC](https://discuss.konghq.com/t/connect-failed-111-connection-refused-while-connecting-to-upstream/3731/9 "2019-06-15T14:29:17Z")

</div>

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
> 
> ```

---

<div class="post-metadata">

**Author:** ![schumannjon](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/schumannjon/32/4078_2.png) [@schumannjon](https://discuss.konghq.com/u/schumannjon)\
**Post date:** [August 14, 2023, 8:58pm UTC](https://discuss.konghq.com/t/connect-failed-111-connection-refused-while-connecting-to-upstream/3731/10 "2023-08-14T20:58:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![3A\_Gefi\_Aulia\_Sanjay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.konghq.com/3a_gefi_aulia_sanjay/32/4612_2.png) [@3A\_Gefi\_Aulia\_Sanjay](https://discuss.konghq.com/u/3A_Gefi_Aulia_Sanjay)\
**Post date:** [August 20, 2024, 4:51am UTC](https://discuss.konghq.com/t/connect-failed-111-connection-refused-while-connecting-to-upstream/3731/11 "2024-08-20T04:51:01Z")

</div>

i encountered same problem, but commenting the ports not solving anything. can u be more specific?
