Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LLSC-32] Add script for starting up a postgres database with docker #10

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions backend/Dockerfile.db
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM postgres:16-alpine

# Set environment variables for PostgreSQL
ENV POSTGRES_USER=${POSTGRES_USER}
ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
ENV POSTGRES_DB=${POSTGRES_DATABASE}

# Add a health check for the PostgreSQL server
HEALTHCHECK --interval=5s --timeout=5s --retries=5 \
CMD pg_isready -U ${POSTGRES_USER} || exit 1

# Expose PostgreSQL port
EXPOSE 5432
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question when we run pdm run dev are we currently connecting to the right db port so that we automatically connect to the db when running locally?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep 5432 is the current default db port.

12 changes: 9 additions & 3 deletions backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,19 @@ To start the backend locally, use the following command:
pdm run dev
```

Note: If you wish to run the backend outside of Docker (e.g., for local development), you'll need to set up a PostgreSQL database. Ensure your database configuration is set properly in the environment variables before running the project. For the time being, the recommended approach for local development using the database is to use the docker compose Postgres instance with your local dev backend.
Note: If you wish to run the backend outside of Docker (e.g., for local development), you'll need to set up a PostgreSQL database. Ensure your database configuration is set properly in the environment variables before running the project.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add some note about setting up the readme properly prior to running this command


To start up the database using docker, run the following command:
```bash
cd backend
pdm run db
```

To check if the database has been started up, type the following:
```bash
docker ps | grep llsc_db
docker ps | grep llsc_db_dev_local
```
This checks the list of docker containers and searchs for the container name `llsc_db`
This checks the list of docker containers and searchs for the container name `llsc_db_dev_local`

## Run Project

Expand Down
1 change: 1 addition & 0 deletions backend/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ precommit = "pre-commit run"
precommit-install = "pre-commit install"
revision = "alembic revision --autogenerate"
upgrade = "alembic upgrade head"
db = "bash start_db_local.sh"

[tool.ruff]
target-version = "py312"
Expand Down
16 changes: 16 additions & 0 deletions backend/start_db_local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

envfile=".env"

# Prioritize base dir .env over backend dir .env, as docker-compose should pull from there
if [ -s "../.env" ]; then
envfile="../.env"
fi

docker build -t llsc_db_dev_local -f Dockerfile.db .
docker run -d \
--name llsc_db_dev_local \
--env-file $envfile \
-p 5432:5432 \
-v llsc_postgres_data:/var/lib/postgresql/data \
llsc_db_dev_local