Getting Started with Docker Containerization for Flask Applications

Getting Started with Docker Containerization for Flask Applications

A Beginner's Guide to Deploying Flask Applications in Docker Containers

Docker is a powerful tool that enables developers to create and deploy applications in containers. Containerization is the process of packaging an application and all its dependencies into a single package, which can be run in any environment without conflicts. In this blog, we will explore the basics of Docker containerization for a Flask application.

Prerequisites:

Before we start, you need to have the following tools installed on your machine:

  • Docker

  • Python and Flask

Step 1: Create a Flask Application

Let's create a simple Flask application that we will containerize with Docker. Create a new directory for the project and navigate to it. Then, create a new file called app.py and add the following code:

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello, World!'

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

This code defines a basic Flask application that serves a "Hello, World!" message on the root route.

Step 2: Create a Dockerfile

Next, we need to create a Dockerfile for our application. A Dockerfile is a script that contains instructions for building a Docker image. Create a new file called Dockerfile in the project directory and add the following code:

# Base image
FROM python:3.8-alpine
# Set the working directory
WORKDIR /app
# Copy the requirements file
COPY requirements.txt .
# Install the dependencies
RUN pip install -r requirements.txt
# Copy the application code
COPY . .
# Expose the port
EXPOSE 5000
# Run the application
CMD ["python", "app.py"]

This Dockerfile uses a base image of Python 3.8 Alpine, which is a lightweight version of the Python image. It sets the working directory to /app, copies the requirements file and installs the dependencies, copies the application code, exposes port 5000, and runs the application using the CMD instruction.

Step 3: Create a requirements.txt file

We also need to create a requirements.txt file that lists all the Python dependencies for our Flask application. Create a new file called requirements.txt in the project directory and add the following code:

Flask==1.1.2

This file lists only one dependency, which is Flask version 1.1.2.

Step 4: Build and Run the Docker Image

Now we are ready to build and run the Docker image. Open a terminal and navigate to the project directory. Run the following command to build the Docker image:

docker build -t myflaskapp .

This command builds a Docker image with the name myflaskapp using the Dockerfile in the current directory. The dot at the end of the command tells Docker to use the current directory as the build context.

After the image is built, we can run it using the following command:

docker run -p 5000:5000 myflaskapp

This command starts a new container from the myflaskapp image and maps port 5000 in the container to port 5000 on the host machine. The Flask application should now be accessible at localhost:5000.

Conclusion:

While this guide covers the basics of Docker containerization, there are many more advanced features and concepts to explore. These include multi-container applications, Docker Compose for orchestration, and Kubernetes for container orchestration at scale.

Docker is an essential tool for modern software development and deployment. It enables developers to create a consistent environment for their applications, making it easier to manage dependencies and ensure application compatibility across different systems.

With the knowledge gained from this guide, you should be well-equipped to start containerizing your Flask applications with Docker. Happy containerizing!

Did you find this article valuable?

Support Opere Peter by becoming a sponsor. Any amount is appreciated!