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:
- Creating Dockerfile
- Building an image based on the Dockerfile
- Pushing the image to Docker Hub repository
- 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.
- 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:
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:
#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:
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.
#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.
We need to set the
DOCKER_HOST environment variable to point to our Azure host OS.
##Verification Verify that host OS is up and running and you are connected to that successfully:
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.
- 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:
##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.
Now we can see the http interface using a browser:
Exposing ports publicly comes with a security threat. Be careful about what your are exposing to the public.
Categories: Azure, Docker, Mongo,