Deploying CRUD Flask Web App With Docker on Azure

Nikhil Singh Kushwah
The Startup
Published in
6 min readAug 22, 2020

--

Intro

This tutorial will be based on creating a CRUD Flask Web app and then deploying it on azure. We will use the PostgreSQL database for this web app. The tutorial has three important parts —

  1. Creating the Web App and testing it locally
  2. Testing locally with database on Azure
  3. Deploying Web App with Docker using Azure DB for PostgreSQL.

What is CRUD

Simple definition for CRUD —

C- Create, R- Read, U- Update, D- Delete

These are the four functionalities that every single web application has. You will see a lot of tutorials for creating a CRUD web app(well, this is also one of that :P). Because generally, this is the next step when you are learning some kind of MVC (Model-View-Controller) framework or programming language.

How to create App

Let’s start, by creating a very simple web app for this. You can clone my Github repo (This repo has the source code to test the webapp locally) and start creating the web app.

Requirements for the Web App:-

Python, Flask, Github, PostgreSQL, Docker

Test locally

After cloning the GitHub, install the requirements. For this, just run the command (All command ran on MacOS).

pip -r install requirements.txt

This command will install all the packages mentioned in the requirements.txt.

Now, create a database of our flask web app.

PostgreSQL

Install PostgreSQL if you don’t have it. Once installed , PostgreSQL creates a default user for you i.e. ‘postgres’.

To create the database, you need to log in as the user and password must be set during the installation process of PostgreSQL. After running this command, enter your password.

psql -U postgres

Let’s create a database named flask_project.

postgres=#  CREATE DATABASE flask_project;

After creating a database we will make a fruits table in which we have three columns — id, fruit, quantity

CREATE TABLE fruits(
id SERIAL PRIMARY KEY,
fruit VARCHAR(255),
quantity VARCAR(255));

Now, test the application by running the command

python app.py

You will see a screen like below and it has all the instructions. To request a server you can use Postman or curl command.

Test locally with database on Azure DB for PostgreSQL

When we tested the application previously, the database was on a local system. Now, we are gonna host our database on Azure.

The following are the steps to create a database server on azure.

  1. Search PostgreSQL and then click on Azure DB for PostgreSQL server.
  2. Click on single server.
  3. Next fill up all the information asked in the form.

Once you are finished with the above steps, then we will start migrating our PostgreSQL database.

Migrate PostgreSQL database

Since we have database right now on our local system, we will use two command-line utility to migrate database to Azure. These are pg_dump and psql.

pg_dump can be used to extract out PostgreSQL database into a script file and then psql will be used to import the data into our azure DB i.e. our target database.

The below command is ran to create a script file that contains the data.

Or I have already provided a database file in the git repository and you can also use that. If you are using the file provided in the repository then just skip the next command and move onto the importing of the database.

pg_dump — host=localhost — username=postgres — dbname=flask_project — file=flask_project.sql

Next, to import the data into the target database, run the command mentioned below.

psql — file=flask_project.sql — host=testserver.database.windows.net — port=5432 — username=login@testserver — dbname=postgres

You can find the information needed to run the above command on the overview page of your Azure database server. Below you can find a screenshot of the overview page.

Run that command and you have successfully imported data into the target database.

Testing connection with Azure db

To test the connection with azure DB, Connection strings are needed to be included in our files. Open database.py, and change the settings with the values you have obtained from the overview page of the azure database. Add sslmode because SSL connections between your server and client applications protect against MITM (man in the middle) attacks.

host = “<server-name>” 
dbname = “<database-name>”
user = “<admin-username>”
password = “<admin-password>”
sslmode = “require”

Once you have updated it, run the command

python app.py

You can test your application once again by using postman or curl command.

Creating Dockerfile and Testing it Locally

I have already included the Dockerfile in the git repository. Or you can create a new file in IDE. Below you can find the Dockerfile for the web app.

FROM python:3.6.1RUN mkdir /codeWORKDIR /codeADD requirements.txt /code/RUN pip install -r requirements.txtADD . /code/# sshENV SSH_PASSWD "root:Docker!"RUN apt-get update \&& apt-get install -y --no-install-recommends dialog \&& apt-get update \&& apt-get install -y --no-install-recommends openssh-server \&& echo "$SSH_PASSWD" | chpasswdCOPY sshd_config /etc/ssh/COPY init.sh /usr/local/bin/RUN chmod u+x /usr/local/bin/init.shEXPOSE 5000 2222#COPY sshd_config /etc/ssh/sshd_configENTRYPOINT ["init.sh"]

After adding the file you can test it locally if the Dockerfile is working or not.

Deploy WebApp on Azure using Docker.

Before deploying it locally we need to configure the application settings in the azure portal and add few lines in our app.py or you can also check out the sample app at GitHub (This repo has the source code for deploying webapp on azure).

Add the lines below in app.py

dbuser=os.environ['db_admin_user']dbpass=os.environ['db_password']dbhost=os.environ['db_server_name']dbname=os.environ['db_name']dbsslmode = os.environ['db_ssl_mode']

Its recommended to keep the database settings in azure portal. As, application settings are directly injected into the docker container as an environment variable automatically.

Next, replace line

conn_string = "host={0} user={1} dbname={2} password={3}".format(host, user, dbname, password)

With the line below. We just need to replace the variable name as we are now getting the database settings from our Azure portal application settings.

conn_string = "host={0} user={1} dbname={2} password={3} sslmode={4}".format(dbhost, dbuser, dbname, dbpass, dbsslmode)

Once you are finished with updating the app.py then we are gonna create a web app on azure using docker. You can follow the steps that I have listed in my previous article.

After creating the web app, click on the configuration in the left pane. And add application settings as shown below.

You need to add these particular settings in configuration and the values for these settings can be found on the overview page of the azure database.

db_admin_user, db_password, db_server_name, db_name, db_ssl_mode

After updating both app.py and application settings in the Azure portal you can stop the app and restart it.

Now you can access the URL provided by azure for your web app and test the CRUD application.

Conclusion

In this article, We learned about how to create a simple CRUD web app and test the web app by deploying it on azure with docker. We also went through various stages of creating an application when a database is hosted locally and when the database is hosted on azure. All feedback and suggestions are appreciated. View the source code used for deploying the Web App here and the source code used for testing the Web App locally here.

--

--

Nikhil Singh Kushwah
The Startup

Azure OSS Engineer | Man United Fan | Sports Analytics