Flask Application with Docker and Docker Compose This repository contains a simple Flask web application that can be containerized using Docker and Docker Compose. Below you'll find information on the key files and how to get started.

Files main.py This is the main Python application file that defines a simple Flask web application.

Flask app: The application uses the Flask framework, a lightweight WSGI web application framework.

Hello World Route:

@app.route("/", methods=['GET']) def hello_world(): return "Hello, World!" This defines a route at the root URL ("/") that responds to HTTP GET requests with the text "Hello, World!".

Running the App:

if name == 'main': app.run(debug=True, host='0.0.0.0') Runs the Flask development server on 0.0.0.0 which makes it accessible from the outside of the container. The debug=True flag enables debug mode for development.

Dockerfile This file defines the steps needed to create a Docker image for the Flask application.

Base Image:

FROM python:3.12-slim Uses an official Python 3.12 slim base image for a minimal footprint.

Set Working Directory:

WORKDIR /app Sets the working directory inside the Docker container to /app.

Install Dependencies:

RUN apt-get update \ && apt-get install -y \ --no-install-recommends \ build-essential \ python3-dev \ ca-certificates \ curl \ && rm -rf /var/lib/apt/lists/* Installs essential libraries and cleans up unnecessary package lists.

Python Packages:

COPY requirements.txt /app/ RUN pip install --no-cache-dir -r requirements.txt Copies requirements.txt into the image and installs the required Python packages.

Copy Application Code:

COPY . /app Copies the entire application code into the /app directory in the container.

Expose Port:

EXPOSE 5000 Exposes port 5000 for external access.

EntryPoint:

ENTRYPOINT ["uwsgi", "--http", "0.0.0.0:5000", "--master", "-p", "2", "-w", "main:app"] USER nobody Sets uwsgi as the entry point, which will run the application using two worker processes. Runs the container as a non-root user for security.

compose.yaml This file is used to define and manage multi-container Docker applications with Docker Compose.

Service Definition:

services: service1: restart: always build: context: . dockerfile: Dockerfile ports: - mode: ingress target: 5000 published: 5000 healthcheck: test: ["CMD", "curl", "-f", "http://localhost:5000/"] Service Configuration:

Service Name: service1. Restart Policy: Always restart the service if it stops. Build Context: build: context: . dockerfile: Dockerfile Uses the Dockerfile in the current directory to build the image.

Port Mapping: ports:

  • mode: ingress target: 5000 published: 5000 Maps port 5000 of the container to port 5000 on the host.

Health Check: healthcheck: test: ["CMD", "curl", "-f", "http://localhost:5000/"] Defines a health check that uses curl to test the application’s / endpoint.

Getting Started Prerequisites Docker Docker Compose Installation Clone the repository:

git clone cd Build and start the Docker container using Docker Compose:

docker-compose up --build Access the application:

Open a web browser and navigate to http://localhost:5000. License This project is licensed under the MIT License. See the LICENSE file for details.

Built With

Share this project:

Updates