aws / aws/aws-sam-cli

--docker-network parmeter not working correctly

Open
#7,732 2 comments 1 reaction 0 assignees View on GitHub
area/docker area/local/start-api stage/bug-repro
Dominant language
Python
Stars
6.7k
Forks
1.2k
Avg merge
1d 10h
Merged PRs (30d)
52

Description

Hello!

### Description:
I'm developing some lambdas with golang, and I'm trying to connect to a postres DB, however, the lambdas are not able to connect to my local development database.
The database is running in another container, but I created a docker network and I haven't had any luck connecting to the DB from the local running lambda.

### Steps to reproduce:
Im creating the network and DB as the following:
```
docker network create my-sam-network

docker volume create postgres_data

docker run -d --name postgres_db_new --restart always --network my-sam-network -e POSTGRES_USER=local_db -e POSTGRES_PASSWORD=local_db -e POSTGRES_DB=vigitrack -p 5432:5432 -v postgres_data:/var/lib/postgresql/data postgres:15
```

Now, to start the API, I'm doing `sam local start-api --docker-network 72a7d526bfc1ea9f0556b8349539d256c7519d05143920087dec0e05f2d73e450`

Then in the lambdas when I invoke them, they say `2024/11/22 18:29:18 Failed to ping the database: dial tcp [::1]:5432: connect: connection refused`

And if I do a `docker network inspect 72a7d526bfc1ea9f0556b8349539d256c7519d05143920087dec0e05f2d73e450` I only see the DB container connected to that network.

### Observed result:

```
2024-11-23 13:08:34,719 | Config file location: C:\Users\Sebassllr\Documents\SecurityApp\VigiTrack-Backend\samconfig.toml
2024-11-23 13:08:34,719 | Config file 'C:\Users\Sebassllr\Documents\SecurityApp\VigiTrack-Backend\samconfig.toml' does not exist
2024-11-23 13:08:34,729 | Using SAM Template at C:\Users\Sebassllr\Documents\SecurityApp\VigiTrack-Backend\.aws-sam\build\template.yaml
2024-11-23 13:08:34,755 | Using config file: samconfig.toml, config environment: default
2024-11-23 13:08:34,755 | Expand command line arguments to:
2024-11-23 13:08:34,755 | --template_file=C:\Users\Sebassllr\Documents\SecurityApp\VigiTrack-Backend\.aws-sam\build\template.yaml --docker_network=72a7d526bfc1ea9f0556b8349539d256c7519d05143920087dec0e05f2d73e45 --host=127.0.0.1 --port=3000 --static_dir=public --layer_cache_basedir=C:\Users\Sebassllr\AppData\Roaming\AWS SAM\layers-pkg --container_host=localhost --container_host_interface=127.0.0.1
2024-11-23 13:08:35,991 | local start-api command is called
2024-11-23 13:08:35,995 | No Parameters detected in the template
2024-11-23 13:08:36,199 | Sam customer defined id is more priority than other IDs. Customer defined id for resource VigiTrack is VigiTrack
2024-11-23 13:08:36,200 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2024-11-23 13:08:36,200 | 0 stacks found in the template
2024-11-23 13:08:36,200 | No Parameters detected in the template
2024-11-23 13:08:36,214 | Sam customer defined id is more priority than other IDs. Customer defined id for resource VigiTrack is VigiTrack
2024-11-23 13:08:36,214 | There is no customer defined id or cdk path defined for resource ServerlessRestApi, so we will use the resource logical id as the resource id
2024-11-23 13:08:36,216 | 2 resources found in the stack
2024-11-23 13:08:36,216 | Found Serverless function with name='VigiTrack' and CodeUri='VigiTrack'
2024-11-23 13:08:36,217 | --base-dir is not presented, adjusting uri VigiTrack relative to C:\Users\Sebassllr\Documents\SecurityApp\VigiTrack-Backend\.aws-sam\build\template.yaml
2024-11-23 13:08:36,266 | Found '7' API Events in Serverless function with name 'VigiTrack'
2024-11-23 13:08:36,266 | Detected Inline Swagger definition
2024-11-23 13:08:36,267 | Lambda function integration not found in Swagger document at path='/items' method='put'
2024-11-23 13:08:36,267 | Lambda function integration not found in Swagger document at path='/items' method='get'
2024-11-23 13:08:36,267 | Lambda function integration not found in Swagger document at path='/user' method='post'
2024-11-23 13:08:36,267 | Lambda function integration not found in Swagger document at path='/user' method='get'
2024-11-23 13:08:36,267 | Lambda function integration not found in Swagger document at path='/hello' method='put'
2024-11-23 13:08:36,267 | Lambda function integration not found in Swagger document at path='/hello' method='post'2024-11-23 13:08:36,267 | Lambda function integration not found in Swagger document at path='/hello' method='get'
2024-11-23 13:08:36,267 | Found '0' APIs in resource 'ServerlessRestApi'
2024-11-23 13:08:36,268 | Removed duplicates from '0' Explicit APIs and '7' Implicit APIs to produce '7' APIs
2024-11-23 13:08:36,268 | 3 APIs found in the template
2024-11-23 13:08:36,275 | Mounting VigiTrack at http://127.0.0.1:3000/hello [GET, POST, PUT]
2024-11-23 13:08:36,275 | Mounting VigiTrack at http://127.0.0.1:3000/items [GET, PUT]
2024-11-23 13:08:36,275 | Mounting VigiTrack at http://127.0.0.1:3000/user [GET, POST]
2024-11-23 13:08:36,275 | You can now browse to the above endpoints to invoke your functions. You do not need to restart/reload SAM CLI while working on your functions, changes will be reflected instantly/automatically. If you used sam build before running local commands, you will need to re-run sam build for the changes to be picked up. You only need to restart SAM CLI if you update your AWS SAM template
2024-11-23 13:08:36,276 | Localhost server is starting up. Multi-threading = True
2024-11-23 13:08:36 WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
* Running on http://127.0.0.1:3000
```

When I call my endpoint I get `2024/11/22 18:29:18 Failed to ping the database: dial tcp [::1]:5432: connect: connection refused`

### Expected result:

I'd except this to be listed under my `docker network inspect` command.
However, it is not showing there. Also I'd expect to be able to connect from the lambda to my DB.

### Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

1. OS: Windows
2. `sam --version`: SAM CLI, version 1.78.0

```
{
"version": "1.78.0",
"system": {
"python": "3.8.8",
"os": "Windows-10-10.0.22621-SP0"
},
"additional_dependencies": {
"docker_engine": "27.3.1",
"aws_cdk": "Not available",
"terraform": "Not available"
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.