September 29, 2023

Festive Fun with Docker

Ho ho ho! So you may have been doodling around with Docker now for a while and maybe you are wondering how you can deploy a docker container containing a web app to the Microsoft Azure platform? This is intended to be a gentle introduction to deploying a docker container in Azure in two different ways. The first is using the Web App for Docker service in Azure. The second is through the Azure Container Instance using the command line. You will see how much more flexibility this approach gives us.

We will deploy a docker container from Docker Hub to our Azure Subscription and perform some post deployment tasks on the container itself.

As an example for this introduction, I found an excellent Secret Santa image by Matt Parlette that looks suitably festive to use. (This guide is written with Matt’s kind permission).

What’s a Azure Webapp and what’s an Azure Container then?

An Azure Webapp lets you make use of the Azure Webapp hosting service – it allows you to deploy webapps via code or containers.

An Azure Container Instance (ACI) is different from a webapp – the web app serves web requests and has application insights and other web related options available for it. The ACI, Azure Container Instance is a lot more flexible with what you can do. It is not just for webapps, but can serve databases, or run complex tasks such as Machine Learning.

What are we building?

We are building a secret Santa website that makes it easy for the management of such an important festive activity. If you work in an office environment and your team are doing a Secret Santa, you can create accounts for everyone who is going to be involved. When you have done this, you can automatically generate who is to buy presents for whom. It’s a nice application, and the Secret Santa application on Docker Hub has enough information on it for us to make use of it. (Unfortunately not all apps on Docker Hub have instructions!)

So let’s begin 🙂

Create a Resource Group

We need to set up a resource group for our ACI to go into. You can do this from the portal, the cloud shell or through the Azure command line. Let’s give the CLI a go – you will need to install the Azure CLI or use the cloud shell to follow along.

To create a resource group,

az group create --name SecretantaRG --location westeurope

The provisioningState should show “Succeeded”

Create Webapp for Container

Azure has a handy “Web App for Containers” option if you have a docker container serving web assets. We can use this option to pull down the web app from the Docker Hub and launch it. There is a bit of configuration after to get this working as expected.

In the Create Web App section, make sure you choose the Docker Container option and set the SKU to the F1 Free Tier.

In the next section, choose Docker Hub as the image source, and point to it using mattparlette/santa.

Click through to Review & Create

Once the deployment is complete, click Go to Resource. Because we have selected the F1 Tier, the initialisation will be quite slow. Under Container Settings, you can view the output of the logs – you will see it downloading and initialising the container. When you see something like “Container secretsantawebapp_0_0c63ce59 for site secretsantawebapp initialized successfully and is ready to serve requests.”, then your website should be accessible.

The running website – you can tell it is an Azure WebApp as it is hosted on the domain.

Create an Azure Container

The second method of deploying the container is using Azure Container Instances. You can now use the az create container command to set up a new container instance within the resource group. The docker hub instructions suggests three environment variables need to be declared when you run the production image. I used a generated key from running the rails secret command on a host with rails installed.

az container create --resource-group SecretSantaRG --name secretsantacnr --image mattparlette/santa --ports 80 3000 --dns-name-label secretsanta --location westeurope --environment-variables 'RAILS_ENV=production' 'RAILS_SERVE_STATIC_FILES=true' 'SECRET_KEY_BASE=d06ba1add7473a4df8135bcec6cd13e120853d4d38df13a739ce4ed723264e9afa245b8860a1672dad87d7e9830a424368be828b837fc691619d979a3209d511'

On success you should see a fair amount of JSON returned, the most important of which is this bit, which shows the IP address of the container on the internet.

Setting up the Admin User

But wait, we are not done yet. The guide on the Docker Hub states that we need to perform a docker exec command – but how can we do this if the container is running on Azure?

With the container now running, we need to add an admin user (as specified on the docker hub page of the project) using the –exec-command to launch inside of the container. Cool!

az container exec --resource-group SecretSantaRG --name secretsantacnr --exec-command /bin/bash

> rails c

> AdminUser.create!(email: '', password: 'password', password_confirmation: 'password')

Now with this done, you should be able to access the admin interface on http://<your-site>

Using the App

The first thing to do as admin is to log into the admin portal and set up some users.

Generate some assignments – this will automatically organise who is to buy a gift for which person.

View assignments – this has some more detail on the individual assignment – If you are participating, I would advise you don’t drill down into this bit!

And now if you log out and log in as a user, you should be presented with the assignment.

On sign in, Connie is present with the user she is to buy a gift for.

Tidying Up

Stop the running container with the following command

az container stop --resource-group SecretSantaRG --name secretsantacnr

Delete the container.

az container delete --resource-group SecretSantaRG --name secretsantacnr

Then in the Azure portal, or through the command line, you can delete your resource group.

Happy Christmas

I hope this has been a nice introduction for you into how you can deploy a simple container or dockerised web-app using Azure.

We also explored how you can drop into the container to execute commands using the az container exec command.

Now you should be able to use the Docker Hub to find your own apps that you wish to deploy.

Have a lovely Christmas!