[Bug]: MongoDB Initialization Script not executed in Coolify Deployment
Description
I'm encountering an issue with MongoDB initialization in my Coolify deployment via Docker compose. The mongo-init.js script is not executing during startup, apparently because it's being interpreted as a directory rather than a file.
Minimal Reproduction (if possible, example repository)
Here is my docker-compose file:
version: '3.8'
services:
mongodb-dev:
image: mongo:latest
container_name: mongodb-dev
restart: always
# env_file: ${ENV_FILE:-.env}
environment:
MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD}
MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
ports:
- '27017:27017'
volumes:
- mongodbdata:/data/db
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro //This should be executed by Mongo during startup
networks:
- my-network
my-nest-backend:
build: .
container_name: my-nest-backend
ports:
- '${PORT}:${PORT}'
- '9229:9229'
depends_on:
- mongodb-dev
networks:
- my-network
# env_file: ${ENV_FILE:-.env}
environment:
DATABASE_NAME: ${DATABASE_NAME}
DATABASE_USER: ${DATABASE_USER}
DATABASE_PASS: ${DATABASE_PASS}
MONGODB_HOST: ${MONGODB_HOST}
NODE_ENV: ${NODE_ENV}
PORT: ${PORT}
MONGO_URI: mongodb://${DATABASE_USER}:${DATABASE_PASS}@${MONGODB_HOST}:27017/${DATABASE_NAME}?authSource=admin&directConnection=true
command: npm run start:debug
volumes:
mongodbdata:
networks:
my-network:
driver: bridge
Exception or Error
By checking the file in the path:
/data/coolify/applications/gg4o4w4ko4okc00cwwso4kg8# cat mongo-init.js
cat: mongo-init.js: Is a directory
Version
v4.0.0-beta.325
Cloud?
- [ ] Yes
- [X] No
try change to
volumes:
-
type: bind
source: ./mongo-init.js
target: /docker-entrypoint-initdb.d/mongo-init.js:ro
try change to
volumes: - type: bind source: ./mongo-init.js target: /docker-entrypoint-initdb.d/mongo-init.js:ro
Unfortunately, it didn't help. The file is still being treated as a directory, and nothing has changed.
@volodymyr-sch
- ok first save file somewhere
- delete the entry from the storages tab
- then go back to compose change to this
volumes:
-
type: bind
source: ./mongo-init.js
target: /docker-entrypoint-initdb.d/mongo-init.js:ro
contents: |
test
- save and go back to storages tab
- remove the word test and paste in actual contents
- save
@volodymyr-sch
- ok first save file somewhere
- delete the entry from the storages tab
- then go back to compose change to this
volumes: - type: bind source: ./mongo-init.js target: /docker-entrypoint-initdb.d/mongo-init.js:ro contents: | test
- save and go back to storages tab
- remove the word test and paste in actual contents
- save
That didn't help either; the file was still treated as a directory. I also tried to handle it manually by:
- Using the 'Convert to file' button
- paste the content of mongo-init.js
- Checking on the machine to see whether changes were applied (they were).
/data/coolify/applications/gg4o4w4ko4okc00cwwso4kg8# ls
README.md docker-compose.yaml mongo-init.js
...
/data/coolify/applications/gg4o4w4ko4okc00cwwso4kg8# cat mongo-init.js
//Shows actual content so changes were appied
const dbName = process.env.DATABASE_NAME;
const dbUser = process.env.DATABASE_USER;
const dbPassword = process.env.DATABASE_PASS;
db = db.getSiblingDB('admin');
try {
db.createUser({
user: dbUser,
pwd: dbPassword,
roles: [
{ role: 'readWrite', db: dbName },
{ role: 'dbAdmin', db: dbName }
]
});
print(`User ${dbUser} created successfully in admin database with access to ${dbName}`);
} catch (error) {
if (error.message.includes('already exists')) {
print(`User ${dbUser} already exists. Skipping creation.`);
} else {
print('Error creating user:', error.message);
throw error;
}
}
- I then redeployed without removing storage, but there were no changes in behavior.