I have two containers linked together by Docker compose and I can successfully ping the container containing MongoDB from the one containing my node.js app using the hostname. However, when it comes to actually connecting to the database, node.js tells me the connection is refused. I am using Docker for Mac if that helps.
mongoose.connect("mongodb://mongo", {server: {auto_reconnect: true}}).catch(error => {
console.log("DB CONNECTION ERROR");
console.log(error)
});
Here is my docker-compose.yml file:
version: "3"
services:
mongo:
image: mongo:latest
volumes:
- ./db:/data/db
restart: always
expose:
- 27017
ports:
- 27017:27017
container_name: mongo
goose:
depends_on:
- mongo
build: .
volumes:
- ./app/server/templates:/usr/src/app/app/server/templates
expose:
- 3005
restart: always
ports:
- 3005:3005
container_name: goose-compose