Deploy a full-fledged Flask app using Docker in Google App Engine

Today we are going to deploy a Flask web app on Google App Engine using Docker container. We will implement a full fledged flask app, not just a single python script app saying 'Hello World', using MySQL database, Google Cloud Datastore and will deploy it on Google Cloud Platform using Docker container. We will try to follow the standard procedure, best practices and security concerns as much as possible to develop a real world app. We will walk through the complete procedure and will try to solve every problem as we face it.

Before we start, I would like to note that the Flask app we are going to deploy today was not only inspired but also lots of code are directly copied from a great youtube tutorial of Corey Schafer. I have changed the code wherever I feel proper as my choice as well as I only developed the signup and login feature excluding the blog feature. I felt that only the login/signup feature would be sufficient enough to get the feel of a real world web application because that already includes the most important parts, like securing the system, database access, sesssion management, email sending, etc. However, I learned Flask app development from Corey's great video tutorial and it deserves mentioning.

The deployed Flask app is running on Just go ahead and have a look at it, check the features. Then we will move forward with the deployment.

Note: If the app is not loading, then it might be the case that the daily expense limit has been reached. I cannot afford the unlimited resource usage on Google cloud platform. So I set daily expense limit of 1 USD 🙂 If the app does not load for you, please check back tomorrow.

If you are new to Flask app development, then go ahead and check Corey's video tutorial. When you're done, then come back. You can also pull the source code from his git repository. If you already have developed a Flask app using MySQL database, then you can follow this tutorial to deploy your already developed app too.

Dockerizing the Flask App

I have created my demo flask app inside a folder called demo-flask-app, so that is the project root folder. This is how the files and folers are organized under project root folder:

└── demo-flask-app
    ├── Dockerfile
    ├── app.yaml
    ├── coreapp
    ├── demo-flask-app-123456789c.json
    ├── migrations
    ├── requirements.txt

While some of the files names are self explanatory, some might need a little bit clarification. The app.yaml describe the app settings for Google App Engine. The coreapp folder contains all the app related files and python scripts including models, routes, templates, static files like html, css, js, etc. As you have already developed a Flask app, you probably know what are a Flask app contents. That demo-flask-app-123456789c.json file is the Google Cloud Platform credential file which let you access different tools and features for cloud platform from inside your app. We will learn more about it and how to get it very soon. I have used Flask-Migrate module as database migration tools. It saves the migration files inside migrations folder. And last but not least, is the entrypoint for the flask app which get executed to start the app.

The requirements.txt file simply lists all the modules we needed to develop our flask app. It can be created using command pip freeze > requirements.txt. We can see the content of our requirements.txt file here.

Next lets have a look at our Dockerfile:

I think the Dockerfile is quite self explanatory and some comments has been added where it might neccessary. We have specified a which executes the needed commands to boostrap the app including perform any pending database migration and then starting the app. Here is our script:

Signup for Google Cloud Platform

First thing first, signup for Google Cloud Platform. With a new signup, you will get a 300 USD free credits to try out different products and features of the platform. Though you have to have register a billing account with a credit card for the identity purpose, you won't be charged anything as long as your free credits are available. Also, the free credit is valid for 1 year. Once your registration is finished, you can see your cloud console at

Create a Project on Google Cloud Platform

Next you have to create a project at Google Cloud Platform. You can create a new project from your cloud console or directly following the link here. I have created a project called Demo Flask App. From now on, whenever we use a tool or feature on the cloud platform, we will by default imply that this Demo Flask App project has been selected on top of the page for any tool/feature we are using.

Create a MySQL Database

Now, we will create a MySQL database on the cloud platform. From our cloud console left menu, we will navigate to SQL > Create Instance and create a MySQL database instance with necessary information. I have created my MySQL database instance with name mysql-demo-flask-app. If we go to the details page of our database instance, we will see all necessary information regarding our database instance including the connection string, IP address and all resource utilization details. From this details page, we can select DATABASES tab on top. Here we can see all the default MySQL databases that comes along with MySQL default installation. We can create here a database for our demo flask app. I have created a database with name db_demo_flask_app.

Create a Google Cloud Datastore

Google Cloud Datastore is a NoSQL database and we will use it to save our environment variables necessary for our app to run. To create a datastore, navigate from the left menu to Datastore > Entities. Here you can create your database manually. But we will use some python scripting to create datastore for local usage as well as sync our local datastore data to cloud platform.

To be continued...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Back To Top