coollabsio / coolify

An open-source & self-hostable Heroku / Netlify / Vercel alternative.
https://coolify.io
Apache License 2.0
34.67k stars 1.89k forks source link

[Bug]: MongoDB Initialization Script not executed in Coolify Deployment #3392

Open volodymyr-sch opened 2 months ago

volodymyr-sch commented 2 months ago

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

image

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?

djsisson commented 2 months ago

try change to

volumes:
  -
    type: bind
    source: ./mongo-init.js
    target: /docker-entrypoint-initdb.d/mongo-init.js:ro
volodymyr-sch commented 2 months ago

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.

djsisson commented 2 months ago

@volodymyr-sch 1) ok first save file somewhere 2) delete the entry from the storages tab 3) 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

4) save and go back to storages tab 5) remove the word test and paste in actual contents 6) save

volodymyr-sch commented 2 months ago

@volodymyr-sch

  1. ok first save file somewhere
  2. delete the entry from the storages tab
  3. 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
  1. save and go back to storages tab
  2. remove the word test and paste in actual contents
  3. save

That didn't help either; the file was still treated as a directory. I also tried to handle it manually by:

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.

<img width="771" alt="image" src="https://github.com/user-attachments/assets/6234c5bf-9d22-4310-97fd-30cb992e564a">