I built a Spring Boot REST API that queries DynamoDB, and I would like to configure one or two Docker containers that run as microservices: one that runs DynamoDB locally and another that runs the REST API. I have a container configured for the Spring Boot REST API, but when I make calls to the API, it cannot query the local DynamoDB even though DynamoDB is running on localhost:8000. The error I get is:
Unable to execute HTTP request: Connect to localhost:8000 [localhost/127.0.0.1] failed: Connection refused (Connection refused)
Despite getting that message, I can access DynamoDB via localhost using my browser. It's as if the Docker container will not allow the API to access localhost:8000 even though the API container is running at localhost:8080.
How should I configure my container(s) so that each runs as a separate microservice? Has anyone seen a similar issue before?
Per default the containers are pretty isolated and runnning within separate networks. You have to link DinamoDB container into Spring Boot container using - - link setting:
https://deis.com/blog/2016/connecting-docker-containers-1/
localhost inside a container doesn't point to real machine host. Literally when you use localhost inside any docker container it will be pointing to the container itself.
Docker provides two ways of handling this sitution:
Links. This is deprecated way and should be avoided in new projects.
Networks. Docker can establish a network where you can access your containers by DNS names (container ID, or container name).
Create a network in a way described here, replace
localhost
by the name of DB container and you'll be ready to go.