From 3ca2a19317816eef4338aa87b72c5f605c2ff65c Mon Sep 17 00:00:00 2001 From: Antonin Filippi Date: Tue, 18 Mar 2025 13:26:50 +0000 Subject: [PATCH] add folders --- z1/README.md | 62 +++++++++++++++++++++++++++++++++++++++++++++++ z1/prepare-app.sh | 13 ++++++++++ z1/remove-app.sh | 11 +++++++++ z1/start-app.sh | 27 +++++++++++++++++++++ z1/stop-app.sh | 6 +++++ 5 files changed, 119 insertions(+) create mode 100644 z1/README.md create mode 100644 z1/prepare-app.sh create mode 100644 z1/remove-app.sh create mode 100644 z1/start-app.sh create mode 100644 z1/stop-app.sh diff --git a/z1/README.md b/z1/README.md new file mode 100644 index 0000000..4dd5b0c --- /dev/null +++ b/z1/README.md @@ -0,0 +1,62 @@ +# Docker Web Application Deployment (Without Docker Compose) + +## Overview +This project deploys a multi-container Docker application consisting of: +- **Flask Web App:** A Python Flask application that connects to a PostgreSQL database. +- **PostgreSQL Database:** Uses the official PostgreSQL image with persistent storage via a named volume. + +Both containers communicate over a custom Docker network (`app_network`). + +## Required Software +- **Operating System:** Linux (with Docker support) +- **Docker:** Installed and configured +- **Bash:** For running the provided scripts + +## Components + +### Flask Web Application +- **Image:** Built locally from the Dockerfile in the `app/` folder. +- **Container Name:** `flask_app` +- **Port Mapping:** Host port `5000` mapped to container port `5000`. +- **Environment Variables:** Set via the `docker run` command to point to the database. + +### PostgreSQL Database +- **Image:** `postgres:13` +- **Container Name:** `postgres_db` +- **Environment Variables:** + - `POSTGRES_USER=user` + - `POSTGRES_PASSWORD=password` + - `POSTGRES_DB=sampledb` +- **Volume:** Named volume `pgdata` is mounted at `/var/lib/postgresql/data` for persistent storage. + +### Virtual Network and Volume +- **Network:** `app_network` – A custom Docker bridge network for inter-container communication. +- **Volume:** `pgdata` – Persists PostgreSQL data between container restarts. + +## How to prepare, run, pause and delete the application +# Step 1: Prepare the application +# This will create the necessary volume, network, and build the Flask image. +./prepare-app.sh + +# Step 2: Run the application +# This will start the PostgreSQL database and Flask web application containers. +./start-app.sh + +# Step 3: Stop the application +# It will stop the containers +./stop-app.sh + +# Step 5: Remove the application +# This will stop and remove the containers, network, and volume. +./remove-app.sh + + +## How to view the application on the web + +# Step 1: Ensure that the Flask application is running +# Make sure that the application has been started by running the ./start-app.sh script. +# The Flask application should be accessible at http://localhost:5000. + +# Step 2: Open a web browser and enter the following URL: +http://localhost:5000 + diff --git a/z1/prepare-app.sh b/z1/prepare-app.sh new file mode 100644 index 0000000..1c34631 --- /dev/null +++ b/z1/prepare-app.sh @@ -0,0 +1,13 @@ +#!/bin/bash +echo "Preparing the application..." + +# Create the Docker volume for PostgreSQL data persistence if it doesn't exist. +docker volume create pgdata + +# Create the custom Docker network (if it already exists, an error is suppressed). +docker network create app_network 2>/dev/null || echo "Network app_network already exists." + +# Build the Flask application image from the Dockerfile in the ./app folder. +docker build -t flask_app_image ./app + +echo "Preparation completed." \ No newline at end of file diff --git a/z1/remove-app.sh b/z1/remove-app.sh new file mode 100644 index 0000000..e28d408 --- /dev/null +++ b/z1/remove-app.sh @@ -0,0 +1,11 @@ +#!/bin/bash +echo "Removing the application..." + +# Remove the containers forcefully. +docker rm -f flask_app postgres_db + +# Remove the custom network and volume if they exist. +docker network rm app_network 2>/dev/null || echo "Network app_network already removed." +docker volume rm pgdata 2>/dev/null || echo "Volume pgdata already removed." + +echo "Application removed." \ No newline at end of file diff --git a/z1/start-app.sh b/z1/start-app.sh new file mode 100644 index 0000000..1ce9508 --- /dev/null +++ b/z1/start-app.sh @@ -0,0 +1,27 @@ +#!/bin/bash +echo "Starting the application" + +# Run PostgreSQL container +docker run -d --name postgres_db \ + --network app_network \ + -e POSTGRES_USER=user \ + -e POSTGRES_PASSWORD=password \ + -e POSTGRES_DB=sampledb \ + -v pgdata:/var/lib/postgresql/data \ + --restart always \ + postgres:13 +# Wait a few seconds to ensure PostgreSQL starts before the Flask app connects. +sleep 10 + +# Run Flask application container +docker run -d --name flask_app \ + --network app_network \ + -p 5000:5000 \ + --restart always \ + -e DB_HOST=postgres_db \ + -e POSTGRES_USER=user \ + -e POSTGRES_PASSWORD=password \ + -e POSTGRES_DB=sampledb \ + flask_app_image + +echo "The application is available at http://localhost:5000" diff --git a/z1/stop-app.sh b/z1/stop-app.sh new file mode 100644 index 0000000..d21d6f5 --- /dev/null +++ b/z1/stop-app.sh @@ -0,0 +1,6 @@ +#!/bin/bash +echo "Stopping the application" + +docker stop flask_app postgres_db + +echo "Application stopped."