Deploying Flask web app with Docker on Azure

Nikhil Singh Kushwah
Analytics Vidhya
Published in
5 min readJul 6, 2020

--

In this article we will be going through a basic explanation of docker, creating a flask web app, and then deploying that web app on azure with docker.

What is Docker

Everyone in software development has heard about docker( or maybe you have heard about containers). Well, before docker every new person in software development was going through the same process — download, extract, install, repeat. Then came DOCKER, it has simplified the whole system dependencies process.The best part is, If it works on local it will work in production. Let’s see what does docker mean:

According to Wikipedia

Docker is a set of platform as a service (PaaS) products that uses OS-level virtualization to deliver software in packages called containers.

Now you know about docker.. right ?? :P

Docker lets you create a specific environment in which your code can run. You can create the same environments multiple times as it allows replica build.

Now that you have an idea about docker lets talk about containers and images.

Container

Docker Container is like a virtual runtime environment that packages all the code and dependencies. It can be an OS oriented container or an application-oriented container.

Image

Docker image is an immutable file that is comprised of multiple layers. The image is built using all the instructions mentioned in the dockerfile. Each dockerfile instruction creates a new layer.

Creating Flask Web App

Let’s start by creating a simple flask web app. Either you can copy-paste the code below or you can clone the web app from my GitHub. These are the steps to create a web app.

Requirements

Python, Flask, GitHub, Docker, Azure

1) Make an empty python file with the choice of your IDE(or you can use visual code).

2) This is a sample app.py

3) Create an index.html with the following code and save it inside a templates folder.

4) Now you can save all the packages that you are using in a requirements.txt. It’s really important to have a requirements.txt in your project folder when you are trying to deploy a web app on azure.

pip freeze > requirements.txt

The command mentioned above will take all the packages that are installed and will store them in requirements.txt. The Better way is to pin your dependencies meaning it should have a version mentioned with the package name.

5) Try to run it locally by running a command

python app.py

6)It should look like this:

Running web app locally with Docker

To run docker locally you must first install docker-desktop from here.

We will be going through various steps to create a dockerfile and also go some information regarding docker commands.

Open your IDE and create a new file.

Below you can find the dockerfile for the web app.

Save it as just Dockerfile with on extensions.

Let’s see what these instructions in dockerfile means. Remember, Each instruction creates one layer.

FROM : This instruction will create a layer from python 3.6.1 image.

RUN : You know what it will do, it will run command. You can perform multiple RUN instructions in a dockerfile.

WORKDIR : WORKDIR will change the directory you are working on. So any instruction after WORKDIR will be performed in that particular directory only.

ADD : It will let you copy the files to a docker image.

EXPOSE : This instruction will expose the port number mentioned in the dockerfile.

ENV : You can set the environment variable by using this instruction.

Docker Commands

We will use a few docker commands to test if our web app is working locally or not. You can run these commands in this sequence and after the last command, you will successfully push the docker image to docker hub registry.

docker build .

This command lets you build an image from the dockerfile. File has to be present in the current directory( That’s what dot refers to). If you want to name your image then you can add -t in the command(for ex:- docker build -t flask-app . )

docker run <image_name>

You can run the image built by docker by running the above-mentioned command.

docker ps

This command will list all the containers running with their name and tag.

docker tag <image_name> <user_name>/<image_name>:<tag>

This command will tag the image with a new name and tag.

docker login

Now you need to login to your docker account. After running this command you need to enter your docker id and password.

docker push <user_name>/<image_name>:<tag>

It will push the built image to the docker hub. We are doing it so that azure can access the image from the docker hub.

Deploy on Azure

To deploy this web app on azure we need to add a few files in our project folder and push it to the docker hub.

First, we will add a file named sshd_config. This file enables ssh and then the client can have a secure connection with the container.

Next up, is a startup script for our docker container in which we will start the ssh server.

After adding all these files our project structure looks like this

Project Structure

Now, we are ready to deploy our flask web app with docker on azure.

Go to your azure portal and click on create a resource.

Next, you will see a similar window like below where you need to enter your details regarding the web app and azure account(you can choose a free tier in SKU and size).

Remember to select Docker Container in Publish option and then click on Next.

Then, A page with options from where azure has to pull the container image will appear. As you have already pushed the docker image to the hub, select Docker Hub as your image source. Then, enter the correct image and tag(for ex:- nikskushwah8/flask-application:update ).

Click on Review+Create and that’s it. You have deployed a fully functional web app with docker on azure.

Conclusion

In this article, We learned about how to create a simple web app on azure with docker. We also enabled ssh on our docker container. In the next blogpost, we will see how to deploy a simple CRUD application on azure with docker.All feedback and suggestions are appreciated. You can view the full code used in this article on my GitHub.

--

--