We have all lamented, with great chagrin, the proverbial "works on my machine" problem because data is different across different development environments.
Wouldn't it be ideal if we could deploy our MongoDB database state from a single repository on all development environments?
In this tutorial, we will be dockerizing a Mongo database with some seed data so we can do just that!
docker-compose.ymlwill orchestrate our two containers; one holding the actual database, the other acting as a client 'seeder' to seed our data.
Dockerfilewill be the Docker file for our custom Mongo client 'seeder'.
start.shwill contain the majority of the work we will have our 'seeder' execute as apart of its
collectionscontains the JSON files representing the seed data we want in our Mongo container.
docker-compose.yml. Edit it so that it has the following:
my_mongo, where our Mongo database will live:
image: mongopulls the Mongo image from Docker Hub
ports:maps local port 27019 to port 27017 (Mongo's default port) in the container. We pick 27019 to avoid conflicting with any Mongo already running on the host machine.
environment:set environment variables for Mongo to configure the root user credentials.
docker-compose up -din the
Fill in connection fields individually, select
authentication: username/password, and fill in the following parameters:
local. Now let's set it up with some seed data!
Dockerfileso that it looks like:
FROM mongopulls the Mongo image from Docker Hub; this gives the container the ability to use a Mongo client to seed the data.
COPY collections/Restaurants.json /collections/Restaurants.jsoncopies the collection JSON into the seeder container.
ADD start.sh /start.shadds the shell script (detailed in Step 4) into the container.
RUN chmod +x /start.shmakes the script executable in the container.
CMD ["/start.sh"]runs the script when the container starts.
my_mongocontainer. Most of this should be self-explanatory:
sleep 10waits for 10 seconds to give
my_mongoenough time to start up.
--host my_mongospecifies the host name of the
my_mongocontainer. Our Docker compose will make sure this resolves!
--db tutorial_dbnames the database we want to put our collection in. It will auto create the DB if it does not exist.
mongo_seederis our container name.
image: mongo_seederwill be the image name for when we build our Dockerfile.
depends_onmakes it so we can access
my_mongoas a host name in
start.shas mentioned in Step 4.
docker build -f Dockerfile -t mongo_seeder .
docker-compose up -d
TonicMongoDockerTutorial_startdirectory with the following contents called
listDatabasesprivilege actions to the
Restarauntscollection, and no privilege to the
Userscollection. This should give us just enough to be able to view only
Restaurantsdocuments, but not edit them in MongoDB Compass.
setupUsers.jsis copied into the container in
mongoshinvokes the Mongo shell with our
lowaccessuserpasswordand see we only have access to view documents on the
setupUsers.jsdoes not have to be just for setting up users. You can do anything there that you can do in the mongo shell.
There we have it! Now all you have to do to reproduce your Mongo state anywhere is pull something like this from a repo onto a machine with Docker and run the commands from Step 6. You can download the complete code here: TonicMongoDockerTutorial_final.zip.
Credit to https://gist.github.com/yoobi55/5d36f13e902a75225a39a8caa5556551 for the Restaurant json data.