Error in MongoDB with docker compose when try to use a command in the YAML file

Viewed 43
version: '3.1'

services:

  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
      MONGO_INITDB_DATABASE: test
    volumes:
        - /home/victor/Desarrollo/docker/mongo:/data/db
        - ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
    ports:
        - "27017:27017"
    command:
        - init-mongo.sh

  mongo-express:
    image: mongo-express
    restart: always
    ports:
      - 8081:8081
    environment:
      ME_CONFIG_MONGODB_ADMINUSERNAME: root
      ME_CONFIG_MONGODB_ADMINPASSWORD: example

and this init-mongo.sh in the same directory

db.auth("root","example")
use test
db.createUser(
  {
    user: "myTester",
    pwd:  "example",   // or cleartext password
    roles: [ { role: "readWrite", db: "test" } ]
  }
)
db.createCollection("collectiontest");

when I build up this, the services wont start, it says its restarting, if I quit the command option from YAML, it works, but I need to create a user and a collection when I install MongoDB with docker.

How can I do that?

Thanks.

0 Answers
Related