Init scripts do not run on mongo container start

Viewed 215

I want to create a MongoDB replica set with a standalone instance running in docker to be able to work with multi-document transactions.

The problem is that the replica set is not initiated automatically, I always have to start it manually, so I can't run it in gitlab runner.

My Dockerfile is as follows:

FROM mongo:4.2

RUN echo "rs.initiate({'_id':'rs0','members':[{'_id':0,'host':'127.0.0.1:27017'}]});" > /docker-entrypoint-initdb.d/replica-init.js

RUN chmod u+x /docker-entrypoint-initdb.d/replica-init.js

RUN cat /docker-entrypoint-initdb.d/replica-init.js

CMD [ "--bind_ip_all", "--replSet", "rs0" ]

And here the docker-compose snippet:

  mongo:
    build:
      context: .
      dockerfile: docker/mongo/Dockerfile
    command: --replSet rs0
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: root
    ports:
      - 27017:27017

I put this script into docker-entrypoint-initdb.d so it should run automatically on first container start but it does not happen.

If I run the same initiate() command manually using mongo shell - it then works fine.

Any ideas on what I do wrong?

0 Answers
Related