Deploying a Docker container in Azure

Azure App Service is a fully managed “Platform as a Service” (PaaS) offering from Microsoft that integrates Websites, Mobile Services, API’s and Logic apps into a single service. Azure App Service gives users various capabilities such as deploy Web and Mobile Applications, API development, Integrating applications with other applications such as Office 365, Dropbox, etc.

With the rise in containerization of applications and in turn, the rise of docker Azure has added docker based deployments as a part of Azure app services. App services offers single and multi-container options. It also allows developers to add public and private docker images from various sources such as docker hub, private registry, azure container registry and an option quick start which provides a fully functional container without the need to write one for your own.

TLDR

Signing in to Azure
Sign in to docker hub
Signing into docker hub on our local machine
Code
Creating an image and container
Test the app locally
Pushing the image to docker hub
Creating the app in Azure
Testing the app
Stop/Delete

Let's get started….

gif from giphy

Signing into Azure

Click on this link to sign in to Azure.
Click on the sign-in button on the top left corner of the screen.
Once you have signed in, Click on portal next to the sign-in button.

Sign in to docker hub

Docker hub is like the GitHub for Docker images, we will be using it to upload our images for the container which is being built in Azure.

Click on this link to sign in to dockerhub

Signing into docker hub on our local machine

Once docker is started in your local machine click on the docker icon present in the taskbar. Then click on “sign in”.

Code

The code can be found in this repository.
Clone it if you would want to follow along.
I used nodejs for my backend framework.

The docker image being used can be found here.

Note: Make sure not to use a predefined port address value, set it as an environment variable instead.

Creating an image and container

Create the “Dockerfile” in the project’s root directory.

If you have never created a docker file before you can refer to my previous article to quickly create one.

To build your image, type the following command in the project’s root directory:

docker image build -t <dockerhub username>/<image name> .

The “-t” flag stands for “tag” it helps give your image a name.
“.” refers to the directory where the “Dockerfile” is present.

The output after I ran the command looks something like this:

docker image build -t mukundhbhushan/azurenodemedium .                                                                                                                                                                              Sending build context to Docker daemon  1.925MB
Step 1/6 : FROM node:alpine
---> 4acd7c5129dc
Step 2/6 : WORKDIR /app
---> Using cache
---> 80e7880d66f7
Step 3/6 : COPY package.json .
---> 9d4ee264d327
Step 4/6 : RUN npm install
---> Running in 226e9ae07f1e
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN dockerazureapp@1.0.0 No description
npm WARN dockerazureapp@1.0.0 No repository field.
added 48 packages from 36 contributors and audited 121 packages in 4.183s
found 0 vulnerabilities
Removing intermediate container 226e9ae07f1e
---> 657675c51199
Step 5/6 : COPY . .
---> 7c44a32ae448
Step 6/6 : CMD ["npm", "start"]
---> Running in 341a5ebadf3f
Removing intermediate container 341a5ebadf3f
---> 9fb1c64483ea
Successfully built 9fb1c64483ea
Successfully tagged azurenode:latest

Ignore the warning

Starting a container

This step is only for local testing purposes and would not effect azure in any way.

To start a container run the following command:

docker container run --name <container name> -p <port used in local machine>:<port running in container> <image name>

“port used in local machine”: port number in which the app runs in our local machine. In my case 8080.

“port running in container”: docker starts the app in the port specified in the program. Type the port value here. In my case, I set the port value to 80.

“image name”: specify the image name after executing the previous step.

My command looks something like this:

docker container run --name azurenode -p 8080:80 mukundhbhushan/azurenode

Its corresponding output:

docker container run --name azurenode -p 8080:80 mukundhbhushan/azurenodemedium
> dockerazureapp@1.0.0 start /app
> node index

Test the app locally

As specified while running the container, navigate to the corresponding port. In my case 8080

Image of app running locally

Now that we know our application is working. Let's get to the Azure side of things.

Pushing the image to docker hub

As we will be using an image from a public dockerhub repository to build our app we must first push the image to dockerhub.

Push the image to dockerhub with the following command:

docker push <docker username>/<image name>

The fields must match the ones filled previously. In my case, the command looks something like this:

docker push mukundhbhushan/azurenodemedium

Now on navigating to your profile on dockerhub, you will see the image added to your public repository.

Creating the app in Azure

Now let's get back to Azure…

Under the “All Services” tab search using the keyword “docker” and choose “App Services”

Now click on “+ Add

A form will appear which we need to fill. Fill in the following details:

Subscription: Let’s azure know on which of the subscription to bill this app. In my case, I chose “Pay-As-You-Go Dev/Test”

Resource group: In a nutshell resources group help in making a logical group of the services that you are using for an application, they can contain web apps, databases or other Azure services such as cognitive services and ML API endpoints, etc. For this application, I chose “Create new”. If you have selected “Create new” you would have to name your resource group. I named mine “dockernode”.

Name: name of the website which will appear in the URL. I named mine “nodedockerapp”

Publish: Choose “Docker”.

Operating system: Choose “Linux”.

Location: choose a location which is nearest to you or the region where the app is going to be deployed. I chose “Australia East”.

Plan: As we chose a new resource group the plan created is also new. This field is automatically filled, edit it if needed.

SKU and Size: This field helps in determining the specifications and the corresponding cost of the chosen configuration. Given that this is a test application that we are building, we must choose “Dev/test”. Depending on which location you have chosen the “FREE F1” plan may or may not be available, feel free to choose the “B1” plan. Otherwise, change the location to the one which supports it.

After filling the form it must look something like this:

Now that the first form is out of the way, Now click on the docker tab.

Fill in the following details:

Option: “Single container” as we are using only a single docker container and not many simultaneously.

Image source: “Docker hub”

Access type: “Public”

Image and tag: paste the name of the image which we pushed to docker hub. The format is: <dockerhub username>/<image name>

Startup command: leave this field empty as we do not want to override any command once the container has spun up.

After filling the form it must look something like this:

The new Azure portal gives you a full-screen view of the current app service being created. Pin it to the dashboard once it has been deployed.

Testing the app

The URL for our app can be found in the overview tab

Once the app is opened in the browser you will observe the URL is not localhost any longer instead it is the app name you have given before.

Stop/Delete

Navigate to the overview tab

Click on Stop/Delete.

Stop: just stop all the processes running the app but you have to pay for other services such as memory and other services running the app. “Stop” can be used when you are adding new features or for other such changes in the apps.

Delete: on the other hand Delete removes all the services and processes working along with the app. You are no longer billed any more for this specific app.

Congratulations YOU DID IT!!!

Thanks for sticking till the end…