From de4f3134f7ee2b25e3fa65254709d4853379ecf3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 19 Mar 2025 17:03:51 +0100 Subject: [PATCH] Initial commit for Docker project --- README.md | 58 ++++++++++++++++++++++++++++++++++++++++++++++ docker-compose.yml | 18 ++++++++++++++ dockerfile | 21 +++++++++++++++++ prepare-app.sh | 11 +++++++++ remove-app.sh | 15 ++++++++++++ start-app.sh | 9 +++++++ stop-app.sh | 9 +++++++ testapp.py | 26 +++++++++++++++++++++ 8 files changed, 167 insertions(+) create mode 100644 README.md create mode 100644 docker-compose.yml create mode 100644 dockerfile create mode 100755 prepare-app.sh create mode 100755 remove-app.sh create mode 100755 start-app.sh create mode 100755 stop-app.sh create mode 100644 testapp.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..65f5fa4 --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +# Docker Web Application + +## Prerequisites +- Docker installed on the system. + +## Application Description +This is a simple Flask web application running inside a Docker container. The app accepts user input via a form and displays the submitted data. + +## Docker Setup + +1. **Preparation** + Run the following command to set up the environment: + ```bash + ./prepare-app.sh + ``` + +2. **Start the Application** + Start the application with: + ```bash + ./start-app.sh + ``` + The app will be available at: [http://localhost:5000](http://localhost:5000) + +3. **Stop the Application** + To stop the application: + ```bash + ./stop-app.sh + ``` + +4. **Remove the Application** + To remove all Docker containers, networks, and volumes: + ```bash + ./remove-app.sh + ``` + +## Working Example +1. Prepare the application: + ```bash + ./prepare-app.sh + ``` +2. Start the app: + ```bash + ./start-app.sh + ``` +3. Visit [http://localhost:5000](http://localhost:5000) in a browser. + +4. Stop the app: + ```bash + ./stop-app.sh + ``` + +5. Remove the app: + ```bash + ./remove-app.sh + ``` + +## Conclusion +This application demonstrates Docker containerization of a Flask app with basic form handling. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..b3dee2a --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,18 @@ +version: '3.8' +services: + flask-app: + build: . + ports: + - "5000:5000" + networks: + - flask-network + restart: unless-stopped + # You can add other services here, like a database if required + +networks: + flask-network: + driver: bridge + +volumes: + flask-data: + driver: local diff --git a/dockerfile b/dockerfile new file mode 100644 index 0000000..c0b3beb --- /dev/null +++ b/dockerfile @@ -0,0 +1,21 @@ +# Use an official Python runtime as a parent image +FROM python:3.8-slim + +# Set the working directory inside the container +WORKDIR /app + +# Copy the current directory contents into the container at /app +COPY . /app + +# Install Flask +RUN pip install --no-cache-dir flask + +# Set the environment variable for Flask +ENV FLASK_APP=testapp.py +ENV FLASK_RUN_HOST=0.0.0.0 + +# Expose port 5000 for the Flask app +EXPOSE 5000 + +# Run the Flask application +CMD ["flask", "run", "--host=0.0.0.0", "--port=5000"] diff --git a/prepare-app.sh b/prepare-app.sh new file mode 100755 index 0000000..a68a539 --- /dev/null +++ b/prepare-app.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +# Create a Docker network +echo "Creating Docker network..." +docker network create flask-network + +# Create a named volume for persistent storage (for example, for database) +echo "Creating Docker volume..." +docker volume create flask-data + +echo "Preparation complete!" diff --git a/remove-app.sh b/remove-app.sh new file mode 100755 index 0000000..c38155e --- /dev/null +++ b/remove-app.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +# Remove the Flask app container +echo "Removing Flask app container..." +docker rm flask-app + +# Remove Docker network +echo "Removing Docker network..." +docker network rm flask-network + +# Remove Docker volume +echo "Removing Docker volume..." +docker volume rm flask-data + +echo "App removed successfully!" diff --git a/start-app.sh b/start-app.sh new file mode 100755 index 0000000..d6d7189 --- /dev/null +++ b/start-app.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +# Start the Flask app container +echo "Starting Flask app container..." +docker run -d --restart unless-stopped --name flask-app --network flask-network -p 5000:5000 flask-app + +# Optionally, start any additional services (like a database) here if needed + +echo "App is running at http://localhost:5000" diff --git a/stop-app.sh b/stop-app.sh new file mode 100755 index 0000000..4f71dbc --- /dev/null +++ b/stop-app.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +# Stop the Flask app container +echo "Stopping Flask app container..." +docker stop flask-app + +# Optionally, stop other services like the database here + +echo "App stopped successfully!" diff --git a/testapp.py b/testapp.py new file mode 100644 index 0000000..06bacea --- /dev/null +++ b/testapp.py @@ -0,0 +1,26 @@ +from flask import Flask, render_template, request + +app = Flask(__name__) + +@app.route('/', methods=['GET', 'POST']) +def home(): + if request.method == 'POST': + college_name = request.form['college_name'] + student_name = request.form['student_name'] + course_name = request.form['course_name'] + year_of_study = request.form['year_of_study'] + + return f"College: {college_name}, Student: {student_name}, Course: {course_name}, Year: {year_of_study}" + + return ''' +
+ College Name:
+ Student Name:
+ Course Name:
+ Year of Study:
+ +
+ ''' + +if __name__ == "__main__": + app.run(debug=True, host="0.0.0.0", port=5000) # This allows the app to run inside the container