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

[Question] Is it possible to connect to a database running on localhost? #2272

Closed
pmtarantino opened this issue Oct 2, 2020 · 10 comments
Closed

Comments

@pmtarantino
Copy link

Hello,

On my team we have several functions running on AWS lambda, and now we want to start testing locally instead of having to deploy everytime we want to test something.

I installing sam-cli, Docker, and finally I was able to make it work on my local (doing sam build and then sam local start-api).

However, the database where these functions writes, are running on localhost (outside the docker machine where are being executed). If I try connecting using localhost, they fail because I suppose they are checking inside the Docker VM.

Is it possible to connect to the hosting machine database? If not, it would be really hard to debug these functions.

(I hope I made myself clear)

@pmtarantino
Copy link
Author

By the way, I tried this #669 (both using --docker-network host and "host.docker.internal") and it didn't work (I am on Windows 10).

@sriram-mv
Copy link
Contributor

@pmtarantino : what version of SAM CLI are you on? can you run your command with --debug. Can you explain what happens when it doesn't work, does it crash, does it stay stuck?

@pmtarantino
Copy link
Author

Current version is 1.4.0. It says it cannot resolve domain name host.docker.internal.

@jfuss
Copy link
Contributor

jfuss commented Oct 6, 2020

@pmtarantino --docker-network host will not work as you expected. On windows, this does not make the service running in the container able to talk to your machine's localhost. It is actually attaching the container running to the VM's localhost.

host.docker.internal should allow the container to talk to you machines localhost. If you are getting domain name not resolvable, it is likely that is is not resolving within docker. Are you running Docker Desktop? This is the way I know about to do in docker. The other thing you could do, is run this database in a docker container and attach them to the same network. Those are the only two options I know about.

Here is the Docker documentation on how to do what you are trying to achieve: https://docs.docker.com/docker-for-windows/networking/#use-cases-and-workarounds

Closing this as it relates to docker and isn't something specific to SAM CLI directly.

@jfuss jfuss closed this as completed Oct 6, 2020
@make-ing
Copy link

make-ing commented Apr 1, 2022

I have the same problem running on Linux. We try to access services running on the locahost (outside of the container) with no success so far. The solution for running a docker container manualy with the option --add-host host.docker.internal:host-gateway works, but it seems not suported by sam. Is there another way to allow the container to access services running on the Linux host?

@nazShg48
Copy link

I have same problems. Can't connect to a PG that is running locally on Linux.
I tried different options for --container-host-interface, --container-host, --docker-network. No success.
SAM CLI, version 1.53.0

Without ability to connect to a local DB there is no sense to use SAM at all, for our team.
Any ideas how to fix this issue?

@nazShg48
Copy link

Downgraded SAM to 1.12.0 and --docker-network host works just fine. But it can not be a right solution to stay with outdated version, please fix this issue.

@bluprince13
Copy link

@jfuss could this be reopened? I don't think there's a straightforward solution right now using SAM CLI. Docker containers allow host.docker.internal to be used to refer to localhost.

Example:

  1. Start a local server
    python3 -m http.server 8000 --bind 127.0.0.1

  2. Spin up a test container and ping local server

docker run -it --add-host=host.docker.internal:host-gateway alpine sh
/ # apk add curl
/ # curl http://host.docker.internal:8000

But, add-host is not an option with SAM CLI.

I also created a question on Stack Overflow about this - https://stackoverflow.com/q/74318930/3521109

@asinghvi-aurora
Copy link

@jfuss Can this be re-opened? As @bluprince13 mentioned we cannot use --add-host with SAM CLI. -add-host is needed on Linux to set up host.docker.internal to point to the docker host.

@DiegoBri11
Copy link

@asinghvi-aurora Added in this PR #6078

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

No branches or pull requests

8 participants