Dockerizing a simple Flask application
When developing applications, a common problem occurs again and again - and its when a teams development environment isn’t similar. This can mean a number of things, as in different operating system, configuration, infrastructure requirements, dependencies, etc. and the list goes on.
In the past, making use of Virtual Machines(VMs) eventually caused some other unbearable issues that was difficult to solve, such as:
- VMs are less efficient because of accessing hardware indirectly
- VMs can be resource intensive
- VMs can be difficult to setup
What is Docker?
And here’s where Docker comes in. Docker is an platform for developing, shopping, and running applocations. It enables the use of separating applications from infrastructure so delivering software can be seamless and quick.
A few terminology and concepts would need to be quickly understood in order to deploy a simply application. An image is a lightweight, stand-aline executable packages that includes everything needed to run a piece of software, code, library, environment variables and config files. A container is a runtime instance of an image. It runs completely isolates form the host environment by default, so accessing host files and ports will need to be configured to do so.
Our Flask application
Let’s create a very simple Flask application so we can see how Docker and containerization works.
First, let’s create a directory and change into it so we can begin creating our flask application.
Next, lets create two files, app.py for our flask application, and requirements.txt. If you don’t know what the requirements.txt file is, it is to specify specific python packages required to run the project.
Next, we will edit both the app.py and requirements.txt file
Next, we will need to create a Dockerfile to help use automate the build process of our application.
A Dockerfile is needed to define what goes on in the environment inside our container. The Dockerfile will contain commands a typical user calls in the command line. Using the docker build command will automate and execute the build process of the Dockerfile.
Let’s create a Dockerfile within our simple-flask directory
Next, we will edit the Dockerfile to build our application
Build our app
Your directory should now look like this:
Next, we run the build command to create a Docker image
Next, we will use the run command to isolate the container and run its own file system, networking, and isoated processes separate from the host.
If we go to 0.0.0.0:5000, this is what should be displayed.
Congratulations, you have succesfully created, ran, pushed, pulled with docker.
This is a very basic introduction to deploying a flask application using docker. It is a very efficient way in deploying an application that has fast startup time, easy to maintain, less resource usage, easier to update, etc, etc.
Just remember, don’t solely rely on containers, VMs still have their place in the world. Having an understanding of both containers and VMs and knowing when to use which one is crucial.