Monday, October 3, 2022
Home CONTAINERIZATION How to containerize a Python Flask app?

How to containerize a Python Flask app?

Intro

Today, developers want to develop, ship, and run applications faster. Docker enables them to build, test, and deploy their code quickly with the containers. In addition, it reduces the time from writing your code to running it in production. This article will describe how to containerize a Python Flask app. We’ll use the Flask framework and Docker for image creation and containerization for the demonstration. You’ll also learn a few commonly used Docker commands.

What is Flask?

Flask is a popular Python micro web framework to develop lightweight web applications and APIs quickly and easily. Moreover, it provides excellent flexibility and scalability for web applications. It is also highly compatible with cutting-edge technologies.

Docker container, image, and dockerfile

To containerize a Python Flask app means running the application in a container. A docker container is a collection of dependencies and code organized to enable applications to run quickly and efficiently in various computing environments.

On the other hand, containers are created using images. A docker image is a blueprint that specifies how to run an application as an isolated process. And, we use Dockerfiles to build images. A Dockerfile is a particular file that includes a set of instructions to build images automatically.

Demo

This demo will proceed over the file structure below to create a Python application first. Follow the steps correctly and have an active virtual environment with Flask installed.

Docker file structure

A. How to create Python (Flask) virtual environment?

1. Create and activate Python virtual environment

  • First, compose a new project directory such as flask-docker and navigate to the newly created directory,
  • Second, create your virtual environment with python3 -m venv venv (LinuxOS),
  • Third, acitivate with . venv/bin/activate command (LinuxOS).

2. Install flask

Install flask with pip install Flask(LinuxOS).

3. Create and intall requirements.txt file

Execute cat > requirements.txt command, paste the following script:

click==8.0.3
Flask==2.0.3
itsdangerous==2.0.1
Jinja2==3.0.3
MarkupSafe==2.0.1
Werkzeug==2.0.3

Ctrl+C to return to prompt.

4. Create and modify ‘app.py’

  • With vim app.py command, create an app.py file and paste the code below into our app.py, save and close it.
from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_guys():
    return '<h1>Hello from Flask-Docker!</h1>'

if __name__ == "__main__":
    app.run(debug=True)

5. Test the application

Now, it is time to run python app.py command to test our Flask app. More or less, you will see the below:

Using the address which our app is broadcasting, for this demo as is curl http://127.0.0.1:5000/, you can see that the flask app say ‘hello’ us:

B. How to containerize a Python Flask app?

Now that we have set up the virtual environment and run our application, we can containerize it.

1. Create and modify a Dockerfile

Execute the command cat > Dockerfile and paste the following script:

# syntax=docker/dockerfile:1

FROM python:3.8-slim-buster
WORKDIR /flask-docker
COPY requirements.txt requirements.txt
RUN pip3 install -r requirements.txt
COPY . /flask-docker
CMD ["python3", "-m" , "flask", "run", "--host=0.0.0.0"]

We reach the final file structure to start to build our image.

2. Build your image

docker build -tag flask-docker . command will build your Docker image for flask application. But, remember executing this command in the same folder which your Dockerfile exist.

3. Run your contianer

After building the image, finally, let’s create our flask-docker container as a last step to containerize a Python Flask app.

docker run -d -p 5000:5000 flask-docker

** On the above command, -d option is for running container detached mode, and -p option is to map container 5000 port to 5000 port of the Docker host machine.

Creating Docker container

4. Test the flask-docker app container

Let’s check our app if it broadcasts on http://127.0.0.1:5000 with curl , or use your web browser.

As a result, our containerized Python Flask app is streaming successfully on port 5000 of Docker host machine.

You can stop your container by:

docker stop <containerID>

If you want, you can delete it by:

docker container rm <containerID>

Conclusion

This article introduced the Python Flask web framework and briefly mentioned Docker containers, images, and Dockerfiles. After that, I have also demonstrated how to create Python Flask virtual environment. Finally, we do practice on how to containerize a Python Flask app.

Thank you for taking the time to read this article. Please ask your question or just leave your comment.

Mustafa Gonen
DevOps engineer, helps companies to balance their needs throughout the software development life cycle, from coding and deployment to maintenance and updates by automation. He believes the beauty of diversity. Working in DevOps culture and being a part of this harmony makes him highly motivated and satisfied.

Leave a Reply

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

Most Popular

How to query EC2 tags from within the instance?

Intro To help you manage your instances, images, and other Amazon EC2 resources, you can assign your own...

How to install Python3 on Amazon Linux 2?

Intro Python is an object-oriented programming language that is widely used. In addition to that, it's free and...

How to connect an AWS EC2 instance with FileZilla and do SFTP?

Intro EC2 is one of the essential services of Amazon Web Services (AWS), providing highly available and scalable...

How to install AWS CLI v2 on Linux?

Intro Whether you’ve been using AWS for some time or you are a novice, you may have noticed...

Recent Comments