Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
Unable to run a script inside docker compose

I have a docker compose file which on up, will be starting a mongo service. What I want to do is as soon as it starts the service, I want to create a database called 'abc' and create a collection 'xyz' into it.

This is what I have tried so far

docker-compose file

services:

  database: 
    image: mongo
    ports:
      - 27017:27017
    command: sh initialSetup.sh

I get the below error

Not sure why is that not able to open the script

enter image description here initialSetup.sh file

#!/bin/sh
mongo < mongoInitialSetup.js

and mongoInitialSetup.js

conn = new Mongo();
db = db.getSiblingDB('abc');
db.createCollection('xyz', {});

Add-on: It would be really great if someone could help me get this done with just the bash script without using the js file

Thanks in advance

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

The below solution worked

services:

  database: 
    image: mongo
    env_file: .env
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_DATABASE: ${DATABASE_NAME}
    volumes:
      - ./initialSetup.sh:/docker-entrypoint-initdb.d/initialSetup.sh

and in the initialSetup.sh, I had

mongo -- "$MONGO_INITDB_DATABASE" <<EOF
    db.createCollection('images', {});
EOF
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!