While running my docker-compose of a frontend, backend and mongo instance, the backend is not able to connect to the mongo db container.
I am using Mongoose and on NestJs on the backend, the connection code looks like this:
const mongoUser = process.env.MONGO_USER;
const mongoPassword = process.env.MONGO_PASSWORD;
const mongoHostname = process.env.MONGO_HOSTNAME;
const mongoPort = process.env.MONGO_PORT;
const url = `mongodb://${mongoUser}:${mongoPassword}@${mongoHostname}:${mongoPort}`;
@Module({
imports: [MongooseModule.forRoot(url, {
useNewUrlParser: true,
"user": process.env.MONGO_USER,
"pass": process.env.MONGO_PASSWORD....
And the corresponding docker-compose.yaml section looks like this:
backend:
container_name: backend
build:
context: ./backend
dockerfile: Dockerfile
ports:
- 4001:4001
depends_on:
- mongo
links:
- mongo
environment:
MONGO_USER: root
MONGO_PASSWORD: example
MONGO_HOSTNAME: mongo
MONGO_PORT: 27017
PORT: 4001
command: npm run start:dev
mongo:
image: mongo
restart: always
ports:
- 27017:27017
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
In the logs of the container I can see a connection is being attempted, but the authentication fails.
Here are the logs. It's strange because the environment ports work and point it at the database, but apparently mongoDB is not recognizing the username and password.
backend | [Nest] 34 - 01/05/2021, 9:09:49 PM [InstanceLoader] JwtModule dependencies initialized +1ms
mongo_1 | {"t":{"$date":"2021-01-05T21:09:49.815+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.18.0.5:47566","connectionId":4,"connectionCount":3}}
mongo_1 | {"t":{"$date":"2021-01-05T21:09:49.847+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn4","msg":"client metadata","attr":{"remote":"172.18.0.5:47566","client":"conn4","doc":{"driver":{"name":"nodejs|Mongoose","version":"3.6.3"},"os":{"type":"Linux","name":"linux","architecture":"x64","version":"4.19.76-linuxkit"},"platform":"'Node.js v14.15.4, LE (unified)","version":"3.6.3|5.11.9"}}}
mongo_1 | {"t":{"$date":"2021-01-05T21:09:49.856+00:00"},"s":"I", "c":"NETWORK", "id":22943, "ctx":"listener","msg":"Connection accepted","attr":{"remote":"172.18.0.5:47568","connectionId":5,"connectionCount":4}}
mongo_1 | {"t":{"$date":"2021-01-05T21:09:49.876+00:00"},"s":"I", "c":"NETWORK", "id":51800, "ctx":"conn5","msg":"client metadata","attr":{"remote":"172.18.0.5:47568","client":"conn5","doc":{"driver":{"name":"nodejs|Mongoose","version":"3.6.3"},"os":{"type":"Linux","name":"linux","architecture":"x64","version":"4.19.76-linuxkit"},"platform":"'Node.js v14.15.4, LE (unified)","version":"3.6.3|5.11.9"}}}
mongo_1 | {"t":{"$date":"2021-01-05T21:09:49.877+00:00"},"s":"I", "c":"ACCESS", "id":20251, "ctx":"conn5","msg":"Supported SASL mechanisms requested for unknown user","attr":{"user":"undefined@admin"}}
mongo_1 | {"t":{"$date":"2021-01-05T21:09:49.877+00:00"},"s":"I", "c":"ACCESS", "id":20249, "ctx":"conn5","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-256","principalName":"undefined","authenticationDatabase":"admin","client":"172.18.0.5:47568","result":"UserNotFound: Could not find user \"undefined\" for db \"admin\""}}
mongo_1 | {"t":{"$date":"2021-01-05T21:09:49.880+00:00"},"s":"I", "c":"ACCESS", "id":20249, "ctx":"conn5","msg":"Authentication failed","attr":{"mechanism":"SCRAM-SHA-1","principalName":"undefined","authenticationDatabase":"admin","client":"172.18.0.5:47568","result":"UserNotFound: Could not find user \"undefined\" for db \"admin\""}}
mongo_1 | {"t":{"$date":"2021-01-05T21:09:49.883+00:00"},"s":"I", "c":"NETWORK", "id":22944, "ctx":"conn4","msg":"Connection ended","attr":{"remote":"172.18.0.5:47566","connectionId":4,"connectionCount":3}}
backend | [Nest] 34 - 01/05/2021, 9:09:49 PM [MongooseModule] Unable to connect to the database. Retrying (1)... +73ms