Reputation: 137
I am trying to connect some app that is a docker-compose service to a MongoDB running in a separate docker container using its own network on the same host machine. What URL should be used by the app to connect to that external network?
My steps...
Created a network and started MongoDB on that network:
docker network create my_app_mongo_db
docker run --name db-mongo -d --network=my_app_mongo_db -p 27017:27017 mongo
Created a docker-compose.yaml
like so:
version: "3"
services:
my_app:
image: my_app
container_name: my_app
networks:
- default
- my_app_mongo_db
networks:
default:
my_app_mongo_db:
external: true
The docker-compose -up
starts fine and docker network inspect my_app_mongo_db
shows that the service is connected to the external network.
Next I am trying to connect to MongoDB using this URL, similarly how I would connect to the DB if it was running as a service:
mongodb://my_app_mongo_db:27017
Yet this approach doesn't work with an external network. What does work is using host.docker.internal
:
mongodb://host.docker.internal:27017
But according to this Docker doc:
This is for development purpose and will not work in a production environment outside of Docker Desktop for Mac.
Any ideas how to resolve this when running on a server?
Upvotes: 2
Views: 483
Reputation: 18578
you need to connect using the Container
name not the network , try this:
mongodb://db-mongo:27017
Upvotes: 1