Sunday, 9 March 2025

AWS SAM local start-api, connecting to db/service when both are running in Docker Desktop

I am current building/testing a node/NestJS based API, that I am deploying to AWS Lambda.
I chose AWS SAM for the deployment tooling, as the integration is simple, lightweight and declarative.

Running the service locally, npm run start:dev, works perfectly and connects to the DB, mongo, hosted in Docker without a problem

Before deploying, I webpack the project to 1 file and run it in a lambda equivalent environment, which also runs on Docker, using "sam local start-api" and here is where the problem appears... 

My service is unable to connect to mongo, when both are running in Docker.

After the usual questions... is MongoDB running, can I connect to it, etc, I knew there was an issue.

Initially all I got was:

Function 'xxxxxx' timed out after 3 seconds

Really helpful.

After some messing around, I eventually reset the SAM timeout, retried and got:
MongoServerSelectionError: connect EHOSTUNREACH 127.0.0.1:27017
Now we are getting somewhere... it is unable to reach mongo.

To cut a long story short, after trying many different things, I eventually found the correct IP to call Mongo running in Docker. It's right down the bottom of the config if you are interested.

More importantly, I then looked up how to extract it directly...
docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' mongodb
Call that, stick the IP address in your environment variables, and tada.. bob's your aunties brother.
Should also work for any other db / service that you need to connect to, when running it all in Docker.

3 comments:

  1. TechMale.com appears to be a personal tech blog with posts about software development, coding tips, debugging and programming insights — mostly written by an individual sharing hands‑on experience.
    It has a simple, old‑school format featuring technical tutorials and problem‑solving posts from various years.
    Content seems more like a developer’s archived notes rather than a mainstream tech news or review site.Digital Marketing Training in Chennai

    ReplyDelete
    Replies
    1. When using AWS SAM CLI with the sam local start-api command, your Lambda functions run inside Docker containers. If your database or dependent services are also running in Docker Desktop, proper networking configuration is essential to allow communication between these containers. By default, containers are isolated, so they cannot directly access each other unless they share a network or are configured correctly. Cloud Computing Projects

      A common approach is to use a shared Docker network. You can create a custom network and run both your database container and SAM containers within it. Then, instead of using localhost, you should reference the database using its container name as the hostname. Alternatively, on systems like Windows and macOS, you can use host.docker.internal to access services running on the host machine. Environment variables should be configured in the SAM template or .env file to store database connection details such as host, port, username, and password. Cloud Security Projects Ensuring correct port mappings, network settings, and service readiness will help establish a reliable connection between your locally emulated API and the database or service running in Docker.

      Delete
  2. Great insights on using AWS SAM Local to start APIs! Your step-by-step approach makes it really easy to follow. For anyone looking to enhance their digital skills alongside cloud knowledge, check out this digital marketing course in Chennai.

    ReplyDelete