-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Can not connect to local database on Ubuntu #4236
Comments
@nazShg48 are you able to connect to your service running on your host from another docker container? Trying to triage if this is environment specific or directly related to interpolation of options supplied to AWS SAM CLI. |
@sriram-mv not sure I understand what I should check. Are you asking me to run my lambda in aws sam and then try and access it from another service that is running in pure docker container (not in aws sam) ? |
Any idea? |
Apologies for the late response, my question is if you are able to access services running within a docker container from the host directly without the use of SAM CLI. example: running postgres on a container and accessing the service from the host. |
I haven't tried it since I need to connect from SAM to local PG instance which is not in docker, but running locally. I and my teammate tried to use SAM on his machine, with Ubuntu 22.04 LTS , same problem. I am pretty sure it is universal problem. |
Also I was able to run dockerized (non SAM) backend server and connect to it from my machine. |
Any updates here? |
Bump |
Yeh, same problem. Trying to connect mysql running on localhost from sam local invoke on Ubuntu 22.04. None of the solutions is working. |
Any idea? I got same problem on ubuntu 20. |
I'm having similar pains. Scenario:
However, when running |
Same problem here |
So far I was not able to fix the issue, but there is a workaround.
after that you can pass this IP to your database URL when you are trying to connect to it
|
I have a similar issue. I am trying to run sam local invoke -e event.json --docker-network host Notice Postgres runs on my host using a simple Docker compose. services:
database:
image: postgres
network_mode: host
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
volumes:
- ./tmp/compose/database/data:/var/lib/postgresql/data Notice I can connect to the database running this command on my machine: psql --host=127.0.0.1 --user=postgres Then I try to connect from another container with docker run --rm --network host -it postgres psql --host=127.0.0.1 --user=postgres --password and it works. My guess is |
Interestingly, down the line I found that the CLI argument sets a
So I tried using the id instead, but that gave me This error led me to #669, and https://github.com/aws/aws-sam-cli/pull/715/files. |
I tried to run with the patch diff --git a/samcli/local/docker/container.py b/samcli/local/docker/container.py
index a34e96c7..2244ffc9 100644
--- a/samcli/local/docker/container.py
+++ b/samcli/local/docker/container.py
@@ -233,6 +233,10 @@ class Container:
# Ex: 128m => 128MB
kwargs["mem_limit"] = "{}m".format(self._memory_limit_mb)
+
+ if self.network_id == "host":
+ kwargs["network_mode"] = self.network_id
+
if self._extra_hosts:
kwargs["extra_hosts"] = self._extra_hosts But I got EDIT: seems to come from aws-sam-cli/samcli/local/docker/container.py Lines 219 to 227 in 5712c7f
which binds at least one port, so of course network mode can't be set to host. |
Here goes nothing 🙂 |
See workaround here: #7278 (comment) |
Thanks @GuillaumeDesforges for providing the solution. In short, create a docker network and connect your local database through the network. Closing as a solution is provided. Feel free to open a new issue if you have any further question. |
|
I get timeout error or
connection to server at "one of the host names that i use" port 5432 failed: Connection refused
when trying to connect to a postgresql that I have locally on my OS, Ubuntu 18.04.6 LTS, from a lambda that is running in sam.In
pg_hba.conf
I set0.0.0.0/0
for all IPv4 connection just to ensure that it is not an issue.Database is working fine as I can use it from other applications that I am running on my local machine, but they are not using sam.
I used all possible combinations for the database host:
172.17.0.1
which is address ofdocker0
,localhost
,127.0.0.1
,host.docker.internal
And commands to run aws sam:
sam local start-api --docker-network host
,sam local start-api
,sam local start-api --container-host localhost --container-host-interface 0.0.0.0 --docker-network host
,sam local start-api --container-host localhost --container-host-interface 0.0.0.0
and some others.Result is the same, either timeout error or
connection to server at {one of the host addresses} port 5432 failed
SAM CLI, version 1.56.1
The text was updated successfully, but these errors were encountered: