3 TIPS TO CREATE AN UNLIMITED FUNDING MINDSET

Money can be a useful resource in your life. It can also make you feel empowered when you have a large sum of money. Then there are those times when money seems like the root of all evil, just simply…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




Deploying Flask web app with Docker on Azure

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.

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

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.

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.

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.

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

6)It should look like this:

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.

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 . )

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.

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.

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.

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.

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.

Add a comment

Related posts:

The coffee shop

A strong smell wafted upwards from the coffee cup. He had been in the coffee shop for 5 hours – waiting all the while. He felt that she’d come by. He swigged all coffee in one gulp. It burned his…

Teaching Is The Definitive Way to Learn

I thought about it when — asked about a book I just finished reading — I wasn’t able to explain the main concepts clearly and smoothly. Understand something doesn’t automatically form culture if you…

How to Run Microsoft Store Games on Steam?

Steam is one of the most renowned gaming platforms over the globe, and reasonably so. What is more, is that you can play MS Store games on it easily. Want to do so? Here is how you can. So, here are…