Skip to main content

Command Palette

Search for a command to run...

Running a Python App in a Docker Container

Updated
Running a Python App in a Docker Container
Y

Tech Lead & Architect | 13+ Years in Cloud, Backend, and AI - Experienced software engineer with expertise in Java, Spring Boot, Microservices, Angular, React, Kafka, DevOps, Python, PySpark, Databricks, and Generative AI. Certified in TOGAF, AWS, and Google Cloud. Passionate about building scalable, secure, and high-performance systems. Enthusiast in Data Engineering & Agentic AI. Author of 1,200+ technical articles sharing insights across diverse tech stacks.

Date: 2022-01-20

Running a Python Flask Application with SQLite in a Docker Container: A Comprehensive Guide

This article details the process of creating a simple Python application using the Flask framework and SQLite database, and then deploying it within a Docker container. We'll explore each step, from setting up the application itself to building and running the Docker image.

Understanding the Components

Before diving into the specifics of the process, let's understand the key technologies involved. Our application uses several crucial components:

  • Python: A versatile and widely-used programming language, forming the foundation of our application's logic.
  • Flask: A lightweight and flexible web framework for Python. Flask helps us structure our application, handle requests, and generate responses. This framework provides a robust and efficient way to create web services.
  • SQLite: A lightweight, file-based database system. SQLite is ideal for smaller applications as it doesn't require a separate database server. It's self-contained and easy to manage, making it perfect for this tutorial. The database is integrated directly into the application, simplifying deployment.
  • Docker: A platform that allows us to package our application and its dependencies into a standardized unit called a container. This ensures that the application runs consistently across different environments, regardless of the underlying operating system or infrastructure. Docker simplifies deployment and helps maintain consistency.

Building the Python Application

Our Python application will consist of several files, each responsible for a specific part of the application's functionality. We'll first create the core of our application. This involves defining the application's structure using the Flask framework, connecting to the SQLite database, and creating controllers to manage data interactions.

The db.py file handles the database connection. It establishes a connection to our SQLite database, songs.db. This file contains the code responsible for setting up and managing the database connection, using the necessary libraries provided by Python and the SQLite library.

The controller.py file acts as an intermediary between the application and the database. This file contains functions that perform Create, Read, Update, and Delete (CRUD) operations on the database. These functions encapsulate the database interactions, allowing the main application logic to remain cleaner and more focused. The functions in this file use SQL queries to interact with the database.

The main.py file is the heart of our application. It uses the Flask framework to define routes (URLs) and handle incoming requests. When a request comes in, this file determines which route to use and then interacts with the database via the functions defined in the controller.py file to fetch and/or manipulate data. It then uses Flask to return a response to the client. This is where the Flask framework's power truly comes into play, managing the communication and the data flow. We also define specific HTTP methods (GET, PUT, DELETE) to interact with the database appropriately for each request.

The requirements.txt file lists all the necessary Python packages required for the application. This file is critical for maintaining the project's dependencies and ensures that the same packages are used during development and deployment. Since SQLite is already included in the standard Python library, it doesn’t need to be listed separately.

Creating the Docker Image

Now that our application is complete, we can package it into a Docker container. This involves creating two essential files: a Dockerfile and a docker-compose.yml file.

The Dockerfile contains instructions on how to build the Docker image. It outlines the steps necessary to create a self-contained environment for our application. This process includes specifying the base image (a pre-built image that contains the necessary operating system and tools), installing Python and any other dependencies listed in the requirements.txt file, copying the application files into the container, setting the working directory, and defining the command to run the application within the container.

The docker-compose.yml file simplifies the process of running multiple Docker containers. In our case, it orchestrates the building of the image (based on the instructions in the Dockerfile) and the creation and running of a container from that image. It specifies the port mapping – linking a port on the host machine (your computer) to a port inside the container. This allows us to access the running application from our browser or other tools. This file streamlines the steps to start and stop the application within a container.

Running the Application

Once the Docker image is built and the container is running, we can access our application through the specified port (in this case, port 8000). We can test the application using tools like Postman to send HTTP requests (GET, PUT, DELETE) and verify that the application responds correctly.

The .dockerignore file, though not explicitly detailed, plays a crucial role in the Docker build process. This file specifies which files or directories should be excluded from the Docker image. This helps reduce the image size and build time by excluding unnecessary files, like temporary files or local configuration details. It is crucial to properly configure this to minimize the image size and build time.

Conclusion

This guide outlines the process of building and deploying a simple Python Flask application using SQLite and Docker. This approach provides several benefits, including improved consistency, portability, and ease of deployment. By packaging the application and its dependencies into a Docker container, we can readily run it on any system supporting Docker, without worrying about conflicts or missing libraries. The modular design of the application, utilizing separate files for database interaction and business logic, makes the code more maintainable and easier to understand. The use of Docker greatly simplifies the deployment process, enabling seamless movement between development, testing, and production environments. This comprehensive approach demonstrates a robust and efficient method for creating and deploying web applications.

Read more

More from this blog

The Engineering Orbit

1174 posts

The Engineering Orbit shares expert insights, tutorials, and articles on the latest in engineering and tech to empower professionals and enthusiasts in their journey towards innovation.