I have a docker-compose.yaml file to start a Dart backend and a MongoDB container. To create a database with a desired name and a user with password, I have a script (see further below).
When running the containers locally on my MacBook everything works as expected. But moving the project to the cloud, the response takes 10seconds or more. I can see that the requests arrive to the api container fast as usual, but for the api to retrieve the data of the db it takes too long.
The graph of the cloud dashboard shows, that the CPU usage is at its limit.
Connecting with MongoDB compass directly to the db in the cloud works fine…
To me it seems that the api and the db keep try to connect at such a high pace that the console is being polluted with logs and is not really scrollable anymore.
Can anybody tell me what am i missing? Maybe some error in the script for db, user and password creation?
docker-compose.yaml
version: "3.8"
services:
backend:
depends_on:
- mongoDb
build: .
ports:
- 8080:8080
restart: unless-stopped
mongoDb:
image: mongo:latest
ports:
- 27017:27017
restart: unless-stopped
command: [ --auth ]
volumes:
- mongo_data:/data/db
- ./entrypoint:/docker-entrypoint-initdb.d
env_file:
- .env
environment:
MONGO_INITDB_ROOT_USERNAME: ${DB_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${DB_PASSWORD}
MONGO_INITDB_DATABASE: ${DB_NAME}
volumes:
mongo_data:
script in entrypoint directory
// Assign env variables
const dbPassword = process.env.DB_PASSWORD;
const dbUsername = process.env.DB_USERNAME;
const dbName = process.env.DB_NAME;
// Connect to the admin database
var admin = db.getSiblingDB("admin");
// Create a new collection within the database
db.createCollection("user");
// Create a admin user
db.createUser(
{
user: dbUsername,
pwd: dbPassword,
roles: [{ role: "root", db: "admin" }]
}
)