My scenario:
I have a server with IP x.x.x.x, on which I run a MongoDB instance. I also build my service with a docker-compose.yml file as below
version: "3"
services:
myApp:
image: myApplication
ports:
- "8080:8080"
environment:
SPRING_DATA_MONGODB_URI: mongodb://localhost:27017/myAppDb
When I run docker-compose up to start the container, I can access my service from my computer via x.x.x.x:8080
But the problem is that my service cannot connect to the mongoDB at localhost:27017, because they're not on the same network. Therefore, I add the option network_mode: "host" to my docker-compose file. It solves the problem, my service can connect to the mongoDB running on my server. However, I cannot access my service via x.x.x.x:8080 anymore
My question:
How can I still connect to my service via x.x.x.x:80 with the option network_mode: "host"? Or is there any way that I don't need to use that option, but my service can still connect to the mongoDB instance?
Note: The MongoDB instance is installed in traditional way (yum install mongodb-org) and set up in a way that it can only be accessed via localhost:27017 (or 127.0.0.1:27107). I don't know why or how but that's the way it is, so I cannot change the connection string to mongodb://x.x.x.x:27017. It just won't work.
I also know that I can add mongodb as a service in the docker-compose file to achieve my goal, but I don't want to do that since in my case, it's better to manage database separately.