1

i'm using this docker-compose.yml to run a local mongo replica set:

version: '3.5'

services:
  mongo1:
    hostname: mongo1
    container_name: localmongo1
    image: mongo
    expose:
      - 27017
    ports:
      - 27017:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
  mongo2:
    hostname: mongo2
    container_name: localmongo2
    image: mongo
    expose:
      - 27017
    ports:
      - 27018:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
  mongo3:
    hostname: mongo3
    container_name: localmongo3
    image: mongo
    expose:
      - 27017
    ports:
      - 27019:27017
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]  

it's simple and works fine, but i do have to: 1- initialize the replica set:

  {
    _id : 'rs0',
    members: [
      { _id : 0, host : "mongo1:27017" },
      { _id : 1, host : "mongo2:27017" },
      { _id : 2, host : "mongo3:27017" }
    ]
  }
)

2- add several users to several dbs :

use db1
db.createUser({  
   user:"user1",
   pwd:"pass",
   roles:[  
      {  
         role:"readWrite",
         db:"db1"
      },
      {  
         role:"dbAdmin",
         db:"db1"
      }
   ]
})

use db2
db.createUser({  
   user:"user2",
   pwd:"pass",
   roles:[  
      {  
         role:"readWrite",
         db:"db2"
      }
   ]
})

i'm trying to find a way to run those last two scripts right after deploying the above 3 containers. should i add another image that runs those scripts? or should be some other way to run those scripts?

(other questions reference to some users' maintained images. i prefer not to use them.)

2bis
  • 55
  • 11

1 Answers1

0

solved it with ps scripts:

Get-Content <file>.txt -raw|docker exec -i localmongo1 mongo
exit 

where <file> is some mongo shell script.

spinkus
  • 7,694
  • 4
  • 38
  • 62
2bis
  • 55
  • 11