X

This site uses cookies and by using the site you are consenting to this. We utilize cookies to optimize our brand’s web presence and website experience. To learn more about cookies, click here to read our privacy statement.

Getting Started with Docker on Azure

Intro & Prereqs

We’re seeing a pretty significant uptick in the use of Azure as the cloud provider of choice among clients. As organizations move to hybrid and/or multi-provider clouds, Docker plays a key role in abstracting underlying platform configuration details away from implementations, allowing developers to build consistently-functioning solutions that can be tested and run in identical configurations, and that can be reliably deployed to disparate environments. In this post we’ll cover running Docker containers on Azure using Docker Machine and using Docker storage volumes for persistent storage.

Since Azure doesn’t yet have a dedicated container service like AWS and GCP, we’ll need to rely on Docker Machine to get the job done. Docker Machine lets us install and control the Docker service on local and remote VMs. We’ll configure a Docker host and use the same Dockerfile we’ve used in previous posts to test our solution. Before we jump in, we’ll need to have the following items installed:

Docker:

Mongo: https://docs.mongodb.com/manual/installation/

  • (the mongod command should be available from your CLI)

MEANjs.org Yeoman Generator: http://meanjs.org/generator.html

Setting up the project

To get started, navigate to an empty directory and generate a new MEAN-stack app using the MEANjs.org Yeoman generator and the following command and options:

Command:

[source language=”bash”]
yo meanjs
[/source]

Options:

[source language=”bash”]
You’re using the official MEAN.JS generator.
? What mean.js version would you like to generate? 0.4.2
0.4.2
? In which folder would you like the project to be generated? This can be changed later. mean
Cloning the MEAN repo…….
? What would you like to call your application? mean-test
? How would you describe your application? Full-Stack JavaScript with MongoDB, Express, AngularJS, and Node.js
? How would you describe your application in comma seperated key words? MongoDB, Express, AngularJS, Node.js
? What is your company/author name? Justin Rodenbostel
? Would you like to generate the article example CRUD module? Yes
? Would you like to generate the chat example module? No
Running npm install for you….
This may take a couple minutes.

——————————————
Your MEAN.js application is ready!

To Get Started, run the following command:

cd mean && grunt

Happy Hacking!
——————————————
[/source]

Next, we need to create directories to house our Mongo data, and we need to start the Mongo server. Navigate to your project directory (using the names above, it should be in the ‘mean’ directory relative to where you ran the last command) and create directories so that the project contains the following folders:

  • <project dir>/data
  • <project dir>/data/db

While in your project directory, start the Mongo server using the following command:

[source language=”bash”]
mongod –dbpath data/db
[/source]

To confirm your database has properly started, you should see output similar to the output below:

[source language=”bash”]
2016-08-24T22:03:19.039-0500 I JOURNAL [initandlisten] journal dir=data/db/journal
2016-08-24T22:03:19.040-0500 I JOURNAL [initandlisten] recover : no journal files present, no recovery needed
2016-08-24T22:03:19.054-0500 I JOURNAL [durability] Durability thread started
2016-08-24T22:03:19.054-0500 I JOURNAL [journal writer] Journal writer thread started
2016-08-24T22:03:19.054-0500 I CONTROL [initandlisten] MongoDB starting : pid=7300 port=27017 dbpath=data/db 64-bit host=Justins-MacBook-Pro.local
2016-08-24T22:03:19.054-0500 I CONTROL [initandlisten]
2016-08-24T22:03:19.054-0500 I CONTROL [initandlisten] ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000
2016-08-24T22:03:19.054-0500 I CONTROL [initandlisten] db version v3.0.7
2016-08-24T22:03:19.054-0500 I CONTROL [initandlisten] git version: nogitversion
2016-08-24T22:03:19.054-0500 I CONTROL [initandlisten] build info: Darwin elcapitanvm.local 15.0.0 Darwin Kernel Version 15.0.0: Wed Aug 26 16:57:32 PDT 2015; root:xnu-3247.1.106~1/RELEASE_X86_64 x86_64 BOOST_LIB_VERSION=1_49
2016-08-24T22:03:19.054-0500 I CONTROL [initandlisten] allocator: system
2016-08-24T22:03:19.054-0500 I CONTROL [initandlisten] options: { storage: { dbPath: "data/db" } }
2016-08-24T22:03:19.060-0500 I INDEX [initandlisten] allocating new ns file data/db/local.ns, filling with zeroes…
2016-08-24T22:03:19.093-0500 I STORAGE [FileAllocator] allocating new datafile data/db/local.0, filling with zeroes…
2016-08-24T22:03:19.093-0500 I STORAGE [FileAllocator] creating directory data/db/_tmp
2016-08-24T22:03:19.190-0500 I STORAGE [FileAllocator] done allocating datafile data/db/local.0, size: 64MB, took 0.096 secs
2016-08-24T22:03:19.215-0500 I NETWORK [initandlisten] waiting for connections on port 27017
[/source]

Now that the database server is running we can start our new application using the following command (again from the project directory):

[source language=”bash”]
grunt
[/source]

To confirm the application is up and running, open a browser window and navigate to http://localhost:3000, where you should see something similar to the screenshot below:

Now that we have a running application, we can start to build out the docker machine we’ll deploy it to.

Provisioning the Azure VM

To start using Azure with Docker, we need to create a Docker Machine configuration that uses a virtual machine in Azure. Using the docker-machine command line tools, we’ll create an Azure Resource Group (complete with a virtual machine, NIC, Firewall, Public IP, Virtual Network, Storage Account and availability set) using the “Azure driver”. It’s pretty simple. Just use the command below:

[source language=”bash”]
docker-machine create -d azure
–azure-ssh-user ops
–azure-subscription-id <azure subscription id>
–azure-open-port 3000
–azure-image canonical:UbuntuServer:14.04.3-LTS:latest
azure-test
[/source]

This will take a few minutes!

Some items to note in the command above:

  • You must replace <azure subscription id> in the command below with your own Azure subscription key
  • We’ve chosen to leave port 3000 open for our application’s development mode
  • We’ve chosen to use the long term support version of Ubuntu 14.04 for our host machine
  • We’ve named the Docker Machine ‘azure-test’

When the machine creation is complete use the following command to verify the Docker Machine is available for configuration.

[source language=”bash”]
docker-machine ls
[/source]

Also make sure your newly created machine is the one that’s currently running with the following command:

[source language=”bash”]
eval $(docker-machine env azure-test)
[/source]

Deployment

Before we go further with Docker, let’s a make a few changes to our app’s Dockerfile so we can run Mongo in our container, and start both the MEANjs.org app and Mongo with one command. In your project’s Dockerfile, replace this line:

[source language=”bash”]
FROM node:0.12
[/source]

which indicates which base image to start with, with these lines:

[source language=”bash”]
FROM node:0.12.14-wheezy

# Install Mongo
RUN apt-key adv –keyserver hkp://keyserver.ubuntu.com:80 –recv EA312927
RUN echo "deb http://repo.mongodb.org/apt/debian wheezy/mongodb-org/3.2 main" | tee /etc/apt/sources.list.d/mongodb-org-3.2.list
RUN apt-get update

RUN apt-get install -y mongodb-org

# Install gem sass for grunt-contrib-sass
RUN apt-get install -y supervisor
[/source]

In this block, we’re performing the following activities:

  • Adding the apt-get repo and relevant keys for the Mongo binary repository
  • Installing Mongo DB using apt-get
  • Installing Supervisord (a lightweight process control system that we’ll configure later) using apt-get

Near the middle of the same file, replace this line:

[source language=”bash”]
WORKDIR /home/mean
[/source]

With these:

[source language=”bash”]
WORKDIR /home/mean

RUN mkdir -p /var/log/supervisor
RUN mkdir -p /data/db
RUN mkdir ./logs

COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
[/source]

In this block, we’re performing the following activities:

  • Creating the log directory for Supervisord
  • Creating the data directory for Mongo DB
  • Creating the logs directory for the MEANjs.org application

Near the bottom of the same file, replace these lines:

[source language=”bash”]
# Port 3000 for server
# Port 35729 for livereload
EXPOSE 3000 35729
CMD ["grunt"]
[/source]

With these:

[source language=”bash”]
EXPOSE 3000
CMD ["/usr/bin/supervisord"]
[/source]

In this block, we’re performing the following activities:

  • Telling Docker to open port 3000 for this image
  • Running Supervisord to “start” the image

You may have noticed that one of the commands we added earlier is copying a file named supervisord.conf from the root of our local project directory to the docker container. Let’s create that file, and add the following content:

[source language=”bash”]
[supervisord]
nodaemon=true

[program:mongod]
command=mongod –dbpath /data/db
redirect_stderr=true

[program:grunt]
command=grunt prod –force
redirect_stderr=true

[/source]

Supervisord is a lightweight process control system commonly used in Linux environments. In this file, we’re instructing supervisord to start mongo and our MEANjs node app in background processes, redirecting their output to the stderr log file.

With a configured docker-machine, we’re ready to build and deploy our containers. Start by using the following command to build your docker image:

[source language=”bash”]
docker build -t mean-test .
[/source]

Next, tag the current build with the following command, replacing <username> with your Docker hub user name. This will assign the image we just assembled the ‘latest’ tag.

[source language=”bash”]
docker tag mean-test <username>/mean-test:latest
[/source]

Next, push the newly tagged image into your repo at Docker hub using the following command:

[source language=”bash”]
docker push <username>/mean-test:latest
[/source]

Now, we can create our machine and storage volume. Use the following command to create a named storage volume using the image you’ve just pushed to Docker Hub, again replacing <username> with your Docker Hub username:

[source language=”bash”]
docker create -v /data –name mean-test-storage <username>/mean-test:latest /bin/true
[/source]

Create the machine, linking to the previously created storage volume and mapping our previously-opened http port (3000), using the command below, again replacing <username> with your Docker Hub username:

[source language=”bash”]
docker run -d -p 3000:3000 –volumes-from mean-test-storage –name mean-test <username>/mean-test:latest
[/source]

Test to see if your app is up and running by navigating to it on the web. If you are not sure what the public IP of your machine you can print the configuration details of your machine in the console by running the following command:

[source language=”bash”]
docker-machine env azure-test
[/source]

Navigate to your site using the public IP and port 3000 and you should see the same screen we saw when you ran the app locally. Pretty easy!

Conclusion

I hope this post has provided a simple overview of how to get started with Docker Machine on Azure, and I hope the use of a full-stack application as an example provides insight beyond the basic tutorials available elsewhere on the web.

Code for this tutorial is available on Github at: https://github.com/jrodenbostel/getting-started-with-docker-on-azure

Time to wrap up some side projects and get back to learning more about using the ELK stack, Elm, and functional programming patterns! Stay tuned.