Many of you are probably familiar with what Docker is and how to use containers, but let me spend a couple of sentences on background anyway. The term containers actually matches the design of the technology itself. It’s like taking all the required components for running an application (a mini virtual machine, the application, and any other necessary tools) and putting them inside a neatly packaged box (i.e. container). Yes, I like metaphors. :)
This architecture makes these containers smaller in size than a full VM counterpart and independent of environment. Containers are composed from a recipe-style file (called a
Dockerfile) so that they can be created and destroyed quickly and consistently using the defined recipe.
To create a container with MongoDB running in it, we will need to create that recipe
Dockerfile. Many companies have eliminated the duplication of creating custom containerized versions of their software by providing
base images - a starter recipe that others can run or use as a foundation. We have done just that in our Dockerfile using MongoDB’s base image.
All of the code for our Docker container is available in the related folder of the Github project.
#Pull base image
LABEL org.opencontainers.image.authors="Jennifer Reif,email@example.com,@JMHReif"
#expose the default port
In the first code block, we are pulling that base image (
mongo) for MongoDB. The next section tells anyone who might use this Dockerfile who maintains it (me). The last couple of lines expose a port in order to connect to the container using MongoDB’s default port is 27017.
Note: If you are particular about file locations, you might want to change the folder paths in the
runMongoDB.sh script to be something specific to your preferences. You’re also welcome to use what I’ve defined, if it doesn’t matter. Also, Windows users will need to adjust the syntax to match Windows file syntax and paths.
Next, we will get the data loaded to our MongoDB instance running in the container.