In the previous part, we created a Docker host OS on Azure. Now that we have a host OS ready, we can create different containers on top of it. In this post we will create a mongodb container and host it on our azure host OS.

There are multiple ways to do this. In here we are trying a more reusable way:

  1. Creating Dockerfile
  2. Building an image based on the Dockerfile
  3. Pushing the image to Docker Hub repository
  4. Run a new container on Azure

#Creating Dockerfile Dockerfile is like a step by step instructions file that tells docker how to create an image. It is easier to maintain, share, and reuse them. There is a complete manual about it here. Github is full of dockerfiles created by others. In this case we are going to dockerize mongodb. I changed an existing dockerfile in this manual.

# Dockerizing Mongodb with ubuntu as base

# Based on ubuntu:latest
FROM ubuntu:latest

MAINTAINER Amir Mohtasebi <amir.mohtasebi@gmail.com>

# Installation based on http://docs.mongodb.org/manual/tutorial/install-mongodb-on-ubuntu/
RUN apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10
RUN echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | tee /etc/apt/sources.list.d/10gen.list

# Installing mongodb-org
RUN apt-get update
RUN apt-get install -y mongodb-org

# Creating db folder
RUN mkdir -p /data/db

# Exposing port to Host OS
EXPOSE 27017
EXPOSE 28017

# Set entry point application
ENTRYPOINT ["/usr/bin/mongod"]
CMD ["--rest", "--httpinterface"]
  • The first line is to define what is the base image for this container
  • In order to use the cached image after the first build, we should maintain the docker consistently. Refer to best practices here.
  • We exposed two ports, 27017, and 28018 for http interface. This means these ports only exposed from container to the host OS and not from host OS to the public yet.
  • Setting the ENTRYPOINT means whenever the container runs, it automatically runs the mongod at the end.
  • I wanted to turn on the rest and http interface by default so I applied those two parameters to mongod using CMD. We could skip that and pass those parameters when we are running the docker container later on.

Save this file as Dockerfile in an empty directory.

#Building an image based on the Dockerfile Now that we have the Dockerfile, we can build the image based on the file. Docker goes through the file and runs the commands line by line. If it needs to load any pre-exisitng image but it is not downloaded yet (in our case latest version of ubuntu), it automatically downloads them from the docker hub.

Go to the directory the file is located, and run the following command:

docker build --tag="[username]/[reponame]" .

Tag the build using the combination of your docker hub username and repository name. You should be able to see the image you just created using the following command:

docker images

#Pushing the image to Docker Hub repository If everything is in order and there is no error in the previous step, we can push this to our docker hub repository. Create an account if you haven’t already and run the following command:

# Enter your credentials
docker login

# Push the image to docker hub repository
docker push "[username]/[reponame]"

Push to Docker Hub

Docker automatically pushes the image to Docker hub. Docker only uploads the images that are not available on the hub. For example ubuntu image won’t be uploaded. At the end we can see the new image in our online repository. Uploading our images to Docker hub lets everyone else search and use it.

Pushed image on Docker Hub

#Run a new container on Azure We can now add a new container based on the pushed image to the Docker Hub to our Azure host OS.

##Configuration We need to set the DOCKER_HOST environment variable to point to our Azure host OS.

export DOCKER_HOST=tcp://bunny-ducker.cloudapp.net:4243

##Verification Verify that host OS is up and running and you are connected to that successfully:

docker --tls info

If the stats are back, it means we are good.

There is a certification issue that may stop us from connecting to the host OS. There is a walkthrough for that here

##Creating the container By running the following command, since we set our docker host to our Azure host OS, it automatically downloads images from our Docker host and creates the container.

docker run -d -p 27017:27017 -p 28017:28017 --name mongodb wizact/mongo
  • It maps port 27017 of container to 27017 of host OS
  • It maps port 27018 of container to 27018 of host OS
  • Set the name of the container to mongodb
  • Uses wizact/mongo repository
  • It runs the container

If we list the running containers we can see the container is running:

docker --tls ps -a

Container running on Azure

##Accessing the Http Interface At the moment we have a container with a running mongod process and a host OS. We mapped the ports we required so that host OS and container can talk to each other. However, those ports are not exposed to public. In order to do that, we can map a port of host OS (e.g. 80) to port 28017 (http interface) of container.

Mapping public ports on Azure

Now we can see the http interface using a browser:

Mongo Http Interface

Exposing ports publicly comes with a security threat. Be careful about what your are exposing to the public.