Skip to content

Commit

Permalink
my commit
Browse files Browse the repository at this point in the history
  • Loading branch information
murad-ali-MoJ committed Dec 17, 2023
1 parent 31fb851 commit 1f4120c
Show file tree
Hide file tree
Showing 5 changed files with 167 additions and 0 deletions.
34 changes: 34 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -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
49 changes: 49 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -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')
32 changes: 32 additions & 0 deletions compose.yaml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions db/password.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mysecretpassword

0 comments on commit 1f4120c

Please sign in to comment.