Skip to content
Advertisement

Connection refused trying to connect to mongoDB docker instance

I’m trying to deploy a simple app with a MongoDB with JPA, but I can’t make it work right.

When I deploy the app in a docker container, I keep getting the following error:

JavaScript

This is the dockerfile I use to build the image:

JavaScript

And the docker-compose.yml I used to run mongoDB:

JavaScript

My entities all follow the following pattern (being the name of the entity):

JavaScript

And my JPA Repositories (as above, being the name of the jpa class & entity):

JavaScript

And finally, I have defined the following properties under application.properties:

JavaScript

I am able to connect to localhost:8081 to see the mongo-express interface, but appart from that, I don’t see anything.

Advertisement

Answer

They can’t connect via loopback address because mongodb and the app are in different containers. They’re like like different machines and localhost for each of them is not the host, but the container itself. Use service name (from docker-compose.yml) instead:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement