Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot call API running on separate localhost port #260

Closed
rhlsthrm opened this issue Jan 11, 2018 · 8 comments
Closed

Cannot call API running on separate localhost port #260

rhlsthrm opened this issue Jan 11, 2018 · 8 comments

Comments

@rhlsthrm
Copy link

I am running another API service on port 3001. When I try to call it from my Lambda local function, I get an error:

2018-01-11T21:16:09.065Z f3abec54-3c7f-1f83-ea7e-691abaf23ed5 (node:1) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejectionid: 2): Error: connect ECONNREFUSED 127.0.0.1:3001

The part of the code in question (using the Axios library):

axios.get('http:https://localhost:3001/hello').then(response => {
  callback(null, {
    statusCode: 200,
    body: JSON.stringify({ message: JSON.stringify(response.data) })
  })
})

It works fine if I use a publicly available endpoint, the issue is only with localhost.

@PaulMaddox
Copy link
Contributor

PaulMaddox commented Jan 14, 2018

This is due to the way Docker works.

When accessing localhost (or 127.0.0.1) from within a Lambda function running in SAM Local, you're actually accessing the docker network, not your laptop/machine.

Try using the main IP address for your local machine instead. If you're using Docker for Mac, you could also use the docker.for.mac.localhost hostname.

I WANT TO CONNECT FROM A CONTAINER TO A SERVICE ON THE HOST
The Mac has a changing IP address (or none if you have no network access). From 17.06 onwards our recommendation is to connect to the special Mac-only DNS name docker.for.mac.localhost which will resolve to the internal IP address used by the host.

source: https://docs.docker.com/docker-for-mac/networking/#use-cases-and-workarounds

@mike-tedford
Copy link

mike-tedford commented Oct 15, 2019

If anyone runs into this while using Docker for Windows, try replacing localhost (or 127.0.0.1) with host.docker.internal

@nilinswap
Copy link

If anyone runs into this while using Docker for Windows, try replacing localhost (or 127.0.0.1) with host.docker.internal

This works for mac too. Thanks.

@guizoxxv
Copy link

For Linux use the IP address of the docker0 interface (172.17.0.1 by default).

@Cpt76
Copy link

Cpt76 commented Jul 12, 2021

For Linux use the IP address of the docker0 interface (172.17.0.1 by default).

Thank you it works on my Ubuntu... but only for port 80, do you know the reason?
I have a local k8s cluster, I can invoke it as localhost:8080.

@TimotheDavid
Copy link

work for me on mnajaroo for minio
thanks very very much :)

@backpackerhh
Copy link

backpackerhh commented Apr 28, 2023

For Linux use the IP address of the docker0 interface (172.17.0.1 by default).

Use following commands in a terminal (choose one) to get that value:

$ docker network inspect bridge -f "{{(index .IPAM.Config 0).Gateway}}"
$ ip address show docker0 | grep -Po 'inet \K[\d.]+'

@xixas
Copy link

xixas commented Mar 14, 2024

For Linux use the IP address of the docker0 interface (172.17.0.1 by default).

Not working for me.
I have a ssh tunnel to EC2 which connects to Neptune DB on port 8182.
localhost:8182/status gives data.
But all combinations which I tried through SAM Local Invoke lambda code fails to connect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants