The purpose of the article is to share my Github project dockerized-nginx-flask-postgres , a dockerized Flask application that starts 3 docker container setup. This article assumes that you have docker and docker-compose installed on your local as you will need these tools to run this project. To run my project
$ git clone my repository and then run the following command in the root directory of the project:
When this command is run a few things will happen:
- docker-compose will build the 3 container setup
- docker-compose will spin up the cluster on one network
- Flask-Migrate will initialize the PostgreSQL database with basic user authentication tables
When Docker finishes initializing the application you will see the following output in of your terminal:
$ ------------------------------------ $ The cluster has been initialized $ Navigate to http://localhost:8080 $ to see the running flask app $ ------------------------------------
Open the browser to http://localhost:8080 and if the compilation of the application was successful then you will see the message Time to get building on the web page.
What’s the Goal of this Project
The goal of this project is to build an adaptable and modular stack that can be configured to be a lean full-stack framework or API depending on your business objectives needs with little to no configuration.
Why I Chose These Technologies
- Docker – Leveraging the modularity of Docker allowed me to structure the application in a way such that anyone who forks my repository can switch out the source code, web server, or database to whatever technologies they feel comfortable using. For example, switching out PostgreSQL for MySQL is as easy as updating the
docker-compose.ymland 1 line of code in the Alembic configuration and that’s it! When technologies change you will be able to swap out any container to stay one step ahead of disruption.
- Nginx – Nginx was specifically written to address the performance limitations of Apache web servers. It can be deployed as a standalone web server or front-end proxy for other servers. Nginx was designed to support high concurrency where each worker process can handle thousands of HTTP connections simultaneously.
- Flask – Most of my experiences in web development have come from working with full-stack frameworks such as Laravel or CakePHP that are out of the box and heavy solutions. Full-stack frameworks are great to have many layers of abstraction for a reason, however, they abstract away a lot of what goes on under the hood. I chose Flask because I have full control of what goes on in my application and only import the libraries that are essential to the application. Flask is everything you need it to be and nothing you don’t want it to be. Building out each feature and functionality from scratch has given me a much better understanding of what happens behind the scenes of full-stack frameworks.
- PostgreSQL – I chose PostgreSQL because it is a powerful open source RDBMS (relational database management system) and I’m most familiar with. Since Flask does not have an ORM (object-relational model) library built in I use Flask-Migrate to manage the evolution of my database.
What’s Next on My TODO List?
- Mapping the hot reload port on the Flask Container to my Local for faster development
- Show how to connect your PostgreSQL container to Navicat for PostgreSQL
- Show configuration for using MySQL instead of PostgreSQL
- Show how to connect a MySQL DB visitation tool (e.g. MySQL Workbench) to the right port on the database docker container
- Build out restful routes to populate the basic user authentication tables
Check it out on Github – I would love to collaborate with anyone! Pass this project along to anyone who you know would benefit from using it or be interested in improving it with me. I will update this blog post as I improve this project.