Introduction
One of the great things about Docker is with little effort you can spin up a container and begin to work with a new technology. In this case, we’ll be spinning up MongoDB within a container using an external path on the host for data storage.
We will introduce you to two Docker concepts: mapping volumes between containers and using a Dockerfile to automate the build.
Create the MongoDB Container
You will want to create a file called Dockerfile with your preferred text editor:
vi Dockerfile
It should be similar to the following:
FROM ubuntu
MAINTAINER [email protected]
RUN \
apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 && \
echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/mongodb.list && \
apt-get update && \
apt-get install -y mongodb-org
VOLUME ["/data/db"]
WORKDIR /data
EXPOSE 27017
CMD ["mongod"]
The RUN instruction will execute the setup steps found on the MongoDB site in their correct order.
The VOLUME instruction will tell the container that ‘/data/db’ is a designated mount point for external hosts or other containers.
EXPOSE instructs the container to expose the given port to the host system.
You would finally conclude with a CMD instruction to run MongoDB.
Save this file and then from the folder where it is saved you will now need to build the container:
docker build -t mymongo .
or if your Dockerfile is stored elsewhere:
docker build -t mymongo /path/to/Dockerfile
You should now be able to issue a docker ps
command and see your Docker image in the list.
Now, just spin up your container:
docker run --name mongo-dev -d -v /opt/mongodb:/data/db -p 27017 mymongo
The -v
switch indicates your our mapping your local host /opt/mongodb
directory with the /data/db
directory within the container. The --name
switch names the running instance.
Run docker ps
to validate the container is running and mapping 27017 to a local port. You should also make note of the local port on the host system to which it is mapped. We’ll need this value in the next step.
Let’s go ahead and ensure we can connect to the server and create a new db with a record. You will need the mongo client tools installed to issue the next commands. Be sure to install the shell and tools match the version of mongo installed in your container:
mongo localhost:49155
Replace 49155
with the local port shown in your docker ps
output.
You should now be at a Mongo prompt.
Create your new database:
use mynewdb
Next, let’s use MongoDB’s sample data script to populate a testData collection:
for (var i = 1; i <= 25; i++) {
db.testData.insert( { x : i } )
}
Query the data:
db.testData.find()
Finally, exit the MongoDB shell:
quit()
Now, let’s just be sure that the instance of Mongo is actually keeping the db files outside of the container by doing a ls
on /opt/mongodb
. You should see something similar to:
journal local.0 local.ns mongod.lock _tmp mynewdb.0 mynewdb.ns
That’s great. Now, what happens when our container is restarted?
docker restart [container_id]
If you get the list of running containers you should now see that the status reflects the container being online for a short amount of time.
Go ahead and re-connect to your MongoDB instance and validate the data is still there.