I recently blogged about how easy it is to use Docker Compose to set up an instance of WordPress. But I was just running that locally on my development box. What if I wanted to get it up and running in the cloud?
Well, one way would be to create a Linux Virtual Machine, install Docker, copy up our
docker-compose.yml file, and run
docker-compose up. But that sounds like quite a bit of work. Can we automate all that?
Well, if we’re using Azure we can make use of ARM templates to deploy a whole Resource Group in one go. An ARM template is a JSON description of all the resources we need including the Virtual Machine itself, the Virtual Network it is on, the Storage Account that will hold its disk images, and so on.
ARM templates make it super easy to deploy cloud infrastructure, and super easy to tear it all down again when we’re done because we can simply delete the resource group.
Not only does this template set up a Ubuntu Virtual Machine and install the Azure Docker Extension onto it, but it includes a basic WordPress Docker compose template that specifies the WordPress and MySql containers very similar to what I showed in my own demo. So this means that as part of the deployment of this template, both containers will be downloaded and started.
Trying this out couldn’t be easier if you have an Azure subscription. And, as I said earlier, it’s very safe since you can clean up after yourself very easily when your done by deleting the Resource Group.
Start off by clicking the helpful “Deploy to Azure” button on the GitHub Repo.
This will take you into the Azure Portal with that template ready to run. At this point you have the option to edit the template, and also to specify any parameters. We need to supply a resource group name and location, and then credentials for MySql and the Linux machine credentials.
This particular script doesn’t give me a choice of what VM size I get – it will make a D1 (3.5GB, 1 Core, SSD disks), which would cost about £40 a month if I left it running.
Once we’ve kicked it off, we’ll see that our Resource Group is deploying:
This can take some time, as there is the time to create and start a new Virtual Machine as well as the time required to get the Docker container images downloaded and running. This template actually creates seven resources:
My deployment however seemed to get stuck. All the resources were present but the site (at dockerwptest.northeurope.cloudapp.azure.com) wasn’t up.
To troubleshoot I needed to ssh into the machine. If you have Git for Windows installed, you can open up the Git Bash prompt and use ssh from there, logging in with the credentials you supplied when deploying the ARM template.
Once into the Virtual Machine a call to
docker ps –a revealed that the containers were both present and the MySql one had been running for some time, but the WordPress one had stopped. I used
docker logs f8f to view the logs of the WordPress container and saw it had failed because it couldn’t contact MySql. So there is a timing issue there on startup, but I could easily restart the WordPress container with a call to
docker start f8f.
This time the WordPress container started, and browsing to my site’s public address showed me the WordPress installation screen:
So, apart from the timing issue with starting the Docker container, this was a remarkably quick and easy way to get some Docker containers deployed to the cloud. And Docker Compose does have some options to auto-restart failed containers which would have been useful in this scenario.
One thing I don’t like about this approach is that we don’t actually provide a
docker-compose.yml file. Instead, the Azure Docker Extension wants the contents of the
docker-compose.yml file to be turned into JSON and placed within the ARM template. And the Azure Docker Extension documentation actually makes it clear that this is not the normal intended way to manage your containers. There are better ways to get your containers running on your VM once you have deployed it.
In conclusion, ARM templates are a great way to get a Docker VM set up, and can also be used to specify an initial set of containers to compose. This is also a really great way to experiment. After getting it all working I was able to tear down the whole thing in minutes, having spent only a few pennies trying this out.