How to create a new Mongodb in a Docker container upon startup?

Really simple Dockerfile:

FROM mongo:3.2.10

#create DB directory
RUN mkdir -p /data/db

EXPOSE 27017

CMD ["mongod"]

When I run the container I want to automatically create a DB called “CBDB”. I do not want to manually run the mongo shell, want the new DB to be created automatically.


code a shell to call the mongo shell ,than use the CMD to call the shell

Could write a that creates the db. You will still be able to specify CMD at run time. While, probably not matter for mongodb.

COPY /usr/local/bin/

EXPOSE 27017
CMD ["mongod"]