Getting Started with Flask and Docker

Getting Started with Flask and Docker

Python is a language that is widely known for its simplicity and powerful features. Flask is a Python framework for building web applications that is lightweight. Docker is a toolkit that developers use to containerize their applications. Combining these three tools makes for an efficient and effective workflow that developers can utilize.

Python

We can first begin with Python. Python is a high-level interpreted language that one can use easily get into with the wide array of documentation that is available to anyone willing to learn. Below is the official documentation to get one started Python.org.

Getting Started

First off we are going to start with setting up our python environment using a virtual environment. We need to create a directory using the following command on your terminal > mkdir flask_api

The Virtual Environment

This command creates a directory next we need to install venv(virtual environment) which will help us with ensuring that our global python environment is not interfered with while we are working. We install the venv using pip3, a package manager for python3 using this command pip3 install venv. After doing this we can create our virtual environment using python3 -m venv flask. This will create our virtual environment named flask. To initialize our virtual environment we use the following command source flask/bin/activate. Now we can install our requirements into our virtual environment using pip3. Use pip3 install flask docker SQLAlchemy. We install flask plus docker and SQLAlchemy an Object Relational Mapper for writing data into our database.

Creating Our First Flask App And Docker File

We create our first app by first creating the file using touch app.py add the following code to the file

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
    return "<p>Hello, World!</p>"

With this code we started by importing flask from Flask then we initialized our app and created a route with a function that will display a paragraph saying Hello World.

Test The App

With the following commands on your terminal, we test whether our application is running

export FLASK_APP=hello
flask run

With this, we can now navigate to this url to confirm that our application is running http://localhost:5000

The Requirements.txt file

With this file, we gather our requirements for the entire application

pip3 freeze > requirements.txt

The Dockerfile

FROM python:3.6.9

COPY requirements.txt requirements.txt

WORKDIR /app

COPY . .

EXPOSE 5000:5000

RUN pip install -r requirements.txt

CMD ['python', 'app.py']

The code in this tells Docker the descriptions to follow while building our image.

Building The Docker Image

Now we can build our docker image

docker build -t helloapp .

Run The Docker Image

docker run -p 5000:5000 -t -i helloapp

Upon running this in your local browser we should go to http://http://127.0.0.1:5000/

Screenshot from 2021-09-16 20-30-18.png

With that, we have created and dockerized our application. Please leave a comment below and connect with me here @KelvinBeno