From 1f4120cae131150c962421fed8a70ee7a2f91980 Mon Sep 17 00:00:00 2001 From: murad-ali-MoJ Date: Sun, 17 Dec 2023 22:05:35 +0000 Subject: [PATCH] my commit --- .dockerignore | 34 +++++++++++++++++++++++++++++++++ Dockerfile | 49 +++++++++++++++++++++++++++++++++++++++++++++++ app.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++ compose.yaml | 32 +++++++++++++++++++++++++++++++ db/password.txt | 1 + 5 files changed, 167 insertions(+) create mode 100644 .dockerignore create mode 100644 Dockerfile create mode 100644 app.py create mode 100644 compose.yaml create mode 100644 db/password.txt diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..3edb0b5 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,34 @@ +# Include any files or directories that you don't want to be copied to your +# container here (e.g., local build artifacts, temporary files, etc.). +# +# For more help, visit the .dockerignore file reference guide at +# https://docs.docker.com/engine/reference/builder/#dockerignore-file + +**/.DS_Store +**/__pycache__ +**/.venv +**/.classpath +**/.dockerignore +**/.env +**/.git +**/.gitignore +**/.project +**/.settings +**/.toolstarget +**/.vs +**/.vscode +**/*.*proj.user +**/*.dbmdl +**/*.jfm +**/bin +**/charts +**/docker-compose* +**/compose* +**/Dockerfile* +**/node_modules +**/npm-debug.log +**/obj +**/secrets.dev.yaml +**/values.dev.yaml +LICENSE +README.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d02bb5b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,49 @@ +# syntax=docker/dockerfile:1 + +# Comments are provided throughout this file to help you get started. +# If you need more help, visit the Dockerfile reference guide at +# https://docs.docker.com/engine/reference/builder/ + +ARG PYTHON_VERSION=3.11.4 +FROM python:${PYTHON_VERSION}-slim as base + +# Prevents Python from writing pyc files. +ENV PYTHONDONTWRITEBYTECODE=1 + +# Keeps Python from buffering stdout and stderr to avoid situations where +# the application crashes without emitting any logs due to buffering. +ENV PYTHONUNBUFFERED=1 + +WORKDIR /app + +# Create a non-privileged user that the app will run under. +# See https://docs.docker.com/develop/develop-images/dockerfile_best-practices/#user +ARG UID=10001 +RUN adduser \ + --disabled-password \ + --gecos "" \ + --home "/nonexistent" \ + --shell "/sbin/nologin" \ + --no-create-home \ + --uid "${UID}" \ + appuser + +# Download dependencies as a separate step to take advantage of Docker's caching. +# Leverage a cache mount to /root/.cache/pip to speed up subsequent builds. +# Leverage a bind mount to requirements.txt to avoid having to copy them into +# into this layer. +RUN --mount=type=cache,target=/root/.cache/pip \ + --mount=type=bind,source=requirements.txt,target=requirements.txt \ + python -m pip install -r requirements.txt + +# Switch to the non-privileged user to run the application. +USER appuser + +# Copy the source code into the container. +COPY . . + +# Expose the port that the application listens on. +EXPOSE 5000 + +# Run the application. +CMD python3 -m flask run --host=0.0.0.0 diff --git a/app.py b/app.py new file mode 100644 index 0000000..68a3da3 --- /dev/null +++ b/app.py @@ -0,0 +1,51 @@ +import json +from flask import Flask +import psycopg2 +import os + +app = Flask(__name__) + +if 'POSTGRES_PASSWORD_FILE' in os.environ: + with open(os.environ['POSTGRES_PASSWORD_FILE'], 'r') as f: + password = f.read().strip() +else: + password = os.environ['POSTGRES_PASSWORD'] + +@app.route('/') +def hello_world(): + return 'Hello, Dockers!!!' + + +@app.route('/widgets') +def get_widgets(): + with psycopg2.connect(host="db", user="postgres", password=password, database="example") as conn: + with conn.cursor() as cur: + cur.execute("SELECT * FROM widgets") + row_headers = [x[0] for x in cur.description] + results = cur.fetchall() + conn.close() + + json_data = [dict(zip(row_headers, result)) for result in results] + return json.dumps(json_data) + + +@app.route('/initdb') +def db_init(): + conn = psycopg2.connect(host="db", user="postgres", password=password) + conn.set_session(autocommit=True) + with conn.cursor() as cur: + cur.execute("DROP DATABASE IF EXISTS example") + cur.execute("CREATE DATABASE example") + conn.close() + + with psycopg2.connect(host="db", user="postgres", password=password, database="example") as conn: + with conn.cursor() as cur: + cur.execute("DROP TABLE IF EXISTS widgets") + cur.execute("CREATE TABLE widgets (name VARCHAR(255), description VARCHAR(255))") + conn.close() + + return 'init database' + + +if __name__ == "__main__": + app.run(host='0.0.0.0') diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 0000000..c13a605 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,32 @@ +services: + server: + build: + context: . + ports: + - 8080:5000 + environment: + - POSTGRES_PASSWORD=mysecretpassword + + db: + image: postgres + restart: always + user: postgres + secrets: + - db-password + environment: + - POSTGRES_DB=example + - POSTGRES_PASSWORD_FILE=/run/secrets/db-password + expose: + - 5432 + healthcheck: + test: ["CMD", "pg_isready"] + interval: 10s + timeout: 5s + retries: 5 + +volumes: + db-data: + +secrets: + db-password: + file: ./db/password.txt diff --git a/db/password.txt b/db/password.txt new file mode 100644 index 0000000..77e51dc --- /dev/null +++ b/db/password.txt @@ -0,0 +1 @@ +mysecretpassword \ No newline at end of file