From fa730cac1b1a476ab6c3b5e122890b64f6e49da3 Mon Sep 17 00:00:00 2001 From: ahoemann Date: Wed, 5 Feb 2025 18:28:17 +0100 Subject: [PATCH] docs: updated for docker; feat: local and latest dockerfiles; add: .dockerignore file; feat: implemented health check for adrianux app --- .dockerignore | 6 ++++++ .gitignore | 1 + README.md | 32 +++++++++++++++++++++++------- app.py | 32 +++++++++++++++++++++--------- docker-compose.yml | 13 ++++++++++++ Containerfile => latest.dockerfile | 0 local.dockerfile | 19 ++++++++++++++++++ 7 files changed, 87 insertions(+), 16 deletions(-) create mode 100644 .dockerignore create mode 100644 docker-compose.yml rename Containerfile => latest.dockerfile (100%) create mode 100644 local.dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c054be6 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,6 @@ +.forgejo +compose.yml +*.dockerfile +.gitignore +LICENSE +README.md diff --git a/.gitignore b/.gitignore index 033df5f..32ac21b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .venv +.env __pycache__ diff --git a/README.md b/README.md index 37b9af4..d8bec04 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,50 @@ # Installation - ## Setting up the environment 1. Create a virtual environment: -```bash +```sh python3 -m venv .venv ``` 2. Activate the virtual environment: -```bash +```sh . .venv/bin/activate ``` 3. Install dependencies: -```bash +```sh pip install requirements.txt ``` +4. Set the environment variables: +```sh +export GIT_URL=http://git.adrianux.net +export WEBSITE_URL=http://adrianux.net +export DEBUGMODE=False +``` + +5. Run the app: +```sh +waitress-serve --listen=0.0.0.0:8080 app:app +``` + +# Docker + ## Building the container -``` -# build -t adrianux: . +```sh +docker build -t adrianux:latest - < latest.dockerfile ``` ## Running the container +```sh +docker run adrianux:latest ``` -# run adrianux: +# Docker Compose + +```sh +docker compose up adrianux ``` diff --git a/app.py b/app.py index 29999af..99b6757 100755 --- a/app.py +++ b/app.py @@ -1,7 +1,10 @@ #!/usr/bin/env python3 from flask import Flask, render_template, redirect, url_for +from concurrent.futures import ThreadPoolExecutor import requests import os +import time + app = Flask(__name__) @@ -19,17 +22,18 @@ def homepage(): @app.route("/monitoring") def monitoring(): - git_url = os.environ['GIT_URL'] - website_url = os.environ['WEBSITE_URL'] - return render_template("monitoring.html", git_status = get_status(git_url), website_status = get_status(website_url) ) + global git_status + global website_status + executor = ThreadPoolExecutor(2) + executor.submit(monitor) + return render_template("monitoring.html", git_status = git_status, website_status = website_status) -def get_status_code(link): - requested_site = requests.get(link) - return requested_site.status_code def get_status(link): + requested_site = requests.get(link) + status_code = requested_site.status_code try: - match get_status_code(link): + match status_code: case 200: return "ONLINE" case _: @@ -37,6 +41,16 @@ def get_status(link): except: return "OFFLINE" +def monitor(): + while True: + git_status = get_status(os.environ['GIT_URL']) + website_status = get_status(os.environ['WEBSITE_URL']) + time.sleep(5) + + + if __name__ == "__main__": - mode = os.environ['DEBUG_MODE'] - app.run(debug=mode) + app.run(debug=os.environ['DEBUG_MODE']) + + + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..731ba82 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,13 @@ +--- + services: + build: + context: "." + dockerfile: "latest.dockerfile" + container_name: "adrianux" + image: "adrianux:latest" + environment: + - "GIT_URL=http://git.adrianux.net" + - "WEBSITE_URL=http://adrianux.net" + - "DEBUG_MODE=False" + ports: + - "127.0.0.1:8080:8080" diff --git a/Containerfile b/latest.dockerfile similarity index 100% rename from Containerfile rename to latest.dockerfile diff --git a/local.dockerfile b/local.dockerfile new file mode 100644 index 0000000..1e8a9f6 --- /dev/null +++ b/local.dockerfile @@ -0,0 +1,19 @@ +FROM alpine:latest +WORKDIR /opt/ + +ENV GIT_URL=https://git.adrianux.net +ENV WEBSITE_URL=https://adrianux.net +ENV DEBUG_MODE=False + +COPY . . + +RUN apk add --no-cache git python3 +RUN python3 -m venv /opt/.venv +RUN /opt/.venv/bin/pip install --no-cache-dir -r requirements.txt +RUN apk del git + +EXPOSE 8080 + +HEALTHCHECK --interval=5m CMD wget --delete-after http://localhost:8080 + +CMD [ "/opt/.venv/bin/waitress-serve", "--listen=0.0.0.0:8080", "app:app" ]