From f43db8ae62d1a8a7f4e74e13d758de23ff3fbe2d Mon Sep 17 00:00:00 2001
From: Yogesh Sharma <yogesh.sharma@catprosystems.com>
Date: Thu, 13 Jun 2024 16:24:53 -0400
Subject: [PATCH] Added files requires to run dev env in a container

---
 Dockerfile            |  8 ++++++++
 README.rst            | 22 ++++++++++++++++++++++
 django_setup_local.sh | 23 +++++++++++++++++++++++
 docker-compose.yml    | 24 ++++++++++++++++++++++++
 settings_local.py     | 18 ++++++++++++++++++
 5 files changed, 95 insertions(+)
 create mode 100644 Dockerfile
 create mode 100755 django_setup_local.sh
 create mode 100644 docker-compose.yml
 create mode 100644 settings_local.py

diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 00000000..87048816
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,8 @@
+FROM python:3.7
+
+WORKDIR /pgweb
+
+COPY requirements.txt .
+RUN pip install -r requirements.txt && apt update && apt install -y postgresql-common && /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh -y && apt install -y postgresql-client-16
+
+CMD ["bash", "/pgweb/django_setup_local.sh"]
diff --git a/README.rst b/README.rst
index c10df08a..5787fbe2 100644
--- a/README.rst
+++ b/README.rst
@@ -35,6 +35,24 @@ Setting up a local instance
 To set up a local instance for testing or building patches, please see the
 `docs/dev_install.rst <docs/dev_install.rst>`_ document.
 
+Setting up dev env using containers and podman
+++++++++++++++++++++++++++++++++++++++++++++++
+
+**Environment:**
+python 3.7
+PostgreSQL 16
+Django as per requirements.txt
+
+PostgreSQL is exposed to host on port 15432 but internally it runs on port 5432.
+Django web is export to host on port 8000
+
+**To start:**
+`podman compose --file docker-compose.yml up --detach`
+
+**To shutdown:**
+
+`podman compose --file docker-compose.yml down`
+
 Licence
 -------
 The code for the website is licensed under
@@ -56,3 +74,7 @@ The icons of Font Awesome are released under the `CC BY 4.0 Licence <https://cre
 The fonts of Font Awesome are released under the `SIL OFL 1.1 License <http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=OFL>`_.
 
 normalize.css uses the `MIT License <https://github.com/necolas/normalize.css/blob/master/LICENSE.md>`_
+
+###
+
+
diff --git a/django_setup_local.sh b/django_setup_local.sh
new file mode 100755
index 00000000..80237edb
--- /dev/null
+++ b/django_setup_local.sh
@@ -0,0 +1,23 @@
+#!/bin/bash
+PG_UP=0
+for w in {1..30}
+do
+  exec 6<> /dev/tcp/postgres/5432
+  if [ $? == 0 ]; then
+    PG_UP=1
+    break
+  else
+    echom "Waiting for Postgres to be up"
+    sleep 1
+  fi
+done
+if [ $PG_UP == 0 ]; then
+  echo "PostgreSQL didn't start"
+  exit 1
+fi
+./manage.py migrate
+PGHOST=postgres PGPORT=5432 PGUSER=pgweb PGPASSWORD=pgweb12345 PGDATABASE=pgweb psql -f sql/varnish_local.sql
+(cd pgweb && find . -name data.json | xargs ../manage.py loaddata)
+./manage.py createsuperuser --username pgwebdevel --email pgwebdevel@example.org --no-input
+./manage.py runserver 0.0.0.0:8000
+
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 00000000..2f0315b6
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,24 @@
+version: "3.7"
+
+services:
+  django:
+    build: .
+    ports:
+      - "8000:8000"
+    volumes:
+      - .:/pgweb:Z
+      - ./settings_local.py:/pgweb/pgweb/settings_local.py:Z
+    links:
+      - postgres
+    depends_on:
+      - postgres
+
+  postgres:
+    image: postgres:16
+    ports:
+      - "15432:5432"
+    environment:
+      POSTGRES_USER: pgweb
+      POSTGRES_PASSWORD: pgweb12345
+      POSTGRES_DB: pgweb
+      #      POSTGRES_HOST_AUTH_METHOD: trust
diff --git a/settings_local.py b/settings_local.py
new file mode 100644
index 00000000..e87b0602
--- /dev/null
+++ b/settings_local.py
@@ -0,0 +1,18 @@
+DEBUG=True
+SITE_ROOT="http://localhost:8000"
+SESSION_COOKIE_SECURE=False
+SESSION_COOKIE_DOMAIN=None
+CSRF_COOKIE_SECURE=False
+CSRF_COOKIE_DOMAIN=None
+
+DATABASES = {
+   'default': {
+       'ENGINE': 'django.db.backends.postgresql',
+       'NAME': 'pgweb',
+       'USER': 'pgweb',
+       'PASSWORD': 'pgweb12345',
+       'HOST': 'postgres',
+       'PORT': '5432',
+   }
+}
+
-- 
2.45.2

