Skip to content

Commit

Permalink
Set up PostgreSQL integration with Django and automated database migr…
Browse files Browse the repository at this point in the history
…ations via entrypoint script
  • Loading branch information
forreya committed Nov 5, 2024
1 parent 0df49d3 commit 8d22492
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.env
.vscode
dist
dist
/db/db_data/*
Binary file modified client/bun.lockb
Binary file not shown.
27 changes: 23 additions & 4 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
version: "3"

services:
frontend:
container_name: beow_frontend
Expand All @@ -12,13 +10,34 @@ services:
volumes:
- ./client:/client # bind mount
- node_modules:/client/node_modules # docker-managed volume
database:
container_name: beow_db
image: postgres:17
ports:
- 5432:5432
env_file:
- .env
environment:
- POSTGRES_DB=beow_db
- POSTGRES_USER=admin
- POSTGRES_PASSWORD=password
- PGUSER=postgres
volumes:
- ./db/db_data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "sh -c 'pg_isready -U admin -d beow_db'"]
interval: 1s
timeout: 3s
retries: 5
backend:
container_name: beow_backend
build:
context: ./server
dockerfile: Dockerfile
ports:
- "2808:2808"

- "2809:2809"
depends_on:
database:
condition: service_healthy
volumes:
node_modules:
4 changes: 3 additions & 1 deletion server/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ RUN pip install -r requirements.txt

COPY . .

CMD ["python3", "manage.py", "runserver", "0.0.0.0:2808"]
RUN chmod +x /server/entrypoint.sh

ENTRYPOINT ["/server/entrypoint.sh"]
7 changes: 7 additions & 0 deletions server/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/sh
until python3 manage.py migrate; do
echo "Updating database schema..."
sleep 1
done

python3 manage.py runserver 0.0.0.0:2809
7 changes: 4 additions & 3 deletions server/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
asgiref==3.7.2
Django==5.0.3
sqlparse==0.4.4
asgiref==3.8.1
Django==5.1.2
sqlparse==0.5.1
psycopg[binary]==3.2.3
9 changes: 7 additions & 2 deletions server/server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,15 @@
# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

# The details below must match the fields in docker-compose.yml
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'beow_db',
'USER': 'admin',
'PASSWORD': 'password',
'HOST': 'database',
'PORT': '5432',
}
}

Expand Down

0 comments on commit 8d22492

Please sign in to comment.