In a previous article, you have created a new web app and deployed it to an Azure Web App instance. In this article, you are going a step further, creating a Docker image for the web app giving more flexibility to managing and deploying it.
Fast-growing business organizations have to rapidly deploy new features of their app if they want to stay on the market.
Deploying an app is not always an easy task because it mainly depends on how the app is structured and what are the tools or deployment patterns used.
In recent years, the rise of container technologies has changed the game: all the complexity related to provisioning infrastructures and install software dependencies faded.
Indeed, there are lots of benefits using them:
- Reduce deployment time;
- Simple hosts configuration;
- Multiple apps can run at the same time using different containers;
- Quick scaling;
- Save infrastructure costs;
- Containers are based on images which are simple to create and manage;
- Containers share the same hardware resources but maintaining isolation among them.
Also, containers differ from Virtual Machines because they have a quick startup and share the resources of the host (avoiding unauthorized access from a container to another). In contrast, containers don't have the same hardware isolation as Virtual Machines.
Docker has prevailed among various container technologies thanks to an entire ready-to-use ecosystem. With it, it is possible to create and distribute an app and execute it on-premises or cloud environments. Specifically, the app is compiled into an image using the Docker CLI: starting from a base image with a specific operating system (i.e., Linux or Windows), Docker will create an image which comprises both the app and the execution environment. The compiled image is then stored in a local or remote repository where can be downloaded and started.
To use Docker, you need to install the Docker Engine on a developer machine: this page shows the supported platforms and the required steps in order to set up your environment. Note that the installation requires a Docker Login account.
After the installation, you can spin up the first container using the Docker CLI:
docker run hello-world
which shows the following:
Also, you can try other useful commands such as:
- docker ps -a: get a list of containers;
- docker image list: get the available images on the local machine;
- docker start <CONTAINER_NAME>: starts a specific container;
- docker stop <CONTAINER_NAME>: stops a specific container;
- docker rm <CONTAINER_NAME>: deletes a container that is not in execution;
- docker image rm <IMAGE_NAME>: deletes a container image;
The build command instead helps to create an image for your app:
docker build <PARAMETERS>
Create a Docker image
In this section, you are going to create a Docker image starting from an example in a previous article.
Create a Dockerfile
First, you need a Dockerfile: this is just a text file containing a list of steps that execute sequentially during the build process.
Creating a Dockerfile in Visual Studio Code is relatively simple: after the Docker Extension installation, the command palette helps generate through a wizard a Dockerfile related to your project. These are the settings:
- Application Platform: ASP.NET Core
- Operating system: Linux
- Port: 80, 443
At the end of the wizard, Visual Studio Code will show a text file like this:
This file is composed of different sections starting with the FROM keyword which defines the base image to build the final image (that includes your app). Other keywords are:
- WORKDIR: set the current directory during the build process;
- EXPOSE: set the ports to listen to incoming connections at runtime;
- COPY: copy files;
- RUN: run commands;
The reason why this Dockerfile has multiple sections is that Docker images can be built using different configurations: for example, in this Dockerfile, you can choose to use the "build" section instead of the "publish" one to compile just the application.
Create a Docker image
To create the image, you can execute the following command in the same directory of the Dockerfile:
docker build -t myfirstapp:v1 .
The "t" parameter stands for tag: this is a fundamental concept of Docker that helps to organize images, and it is composed of the name and the version of your app. Usually, the last built image is identified as the latest. The dot (.) parameter represents the local directory. If you execute the command docker image list, you should see the new image is on the local repository list. The app can be started on the local pc using the command:
docker run -p 8080:80 -d --name CONTAINER-NAME IMAGE_NAME
But since we want to start the container on Microsoft Azure, we need to upload the image on a container registry such as DockerHub. As told before, when you install Docker, a Docker Login is required: this gives you access to free repositories on DockerHub.
To create an image for DockerHub, you need to tag it using your DockerHub username:
docker build -t <dockerhubusername>/myfirstapp:v1 .
Finally, push the image using:
docker push <dockerhubusername>/myfirstapp:v1
Start the image on an Azure Web App
To host the container, go to the Microsoft Azure Portal and create an Azure Web App (like in the previous article) and select Docker Container for the Publish parameter:
Clicking "Next Docker", will show the Docker image configuration:
change the configuration with these settings:
- Options: Single Container;
- Image Source: Docker Hub;
- Access Type: Public;
- Image and Tag: the image name and tag created before.
Next, click to "Review + Create" and then "Create". Microsoft Azure will start the resource provisioning, and once completed, you can navigate to the Azure Web App address and check that the application will run correctly.
Start the image on an Azure Container Instance
Another way to deploy a container in Microsoft Azure is Azure Container instance (from now on ACI). This service allows you to spin up a container in a few steps guaranteeing the perfect isolation for your application and expediting the app's scaling. Also, pricing is appealing because you pay only for the time the container is up.
To create an Azure Container instance in the Azure portal, create a new resource using the side menu. In the search box, type "Container Instances", and then "Create".
Fill the ACI settings such as the following image, changing the name of the image with the one you created:
After, click "Review + Create" and then "Create". When the provisioning completes, check the settings page of your ACI to get the IP address assigned and visit the running app.
Until now, we have seen a simple case scenario when you have just one image, but if we have multiple images that work together, you need an orchestrator like Kubernetes that helps you provision, manage, and scale. A forthcoming article will cover this topic talking about Azure Kubernetes Service.