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

Update docker compose to use unified db container #4

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 10 additions & 11 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
version: "3"

services:
database-course:
image: postgres:alpine
database:
image: pgvector/pgvector:pg16
restart: always
expose:
- 2032
- 5432
ports:
- "2032:5432"
- "5432:5432"
volumes:
- coursedata:/var/lib/postgresql/data
- dbdata:/var/lib/postgresql/data
- ./../course_service/pg-init-scripts:/docker-entrypoint-initdb.d
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root
- POSTGRES_DB=course_service
- POSTGRES_CREATE_DB_COURSE_SERVICE=course_service
app-course:
build:
context: ./../course_service
Expand All @@ -27,9 +26,9 @@ services:
- "2000:2000"
- "2001:2001"
depends_on:
- database-course
- database
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://database-course:5432/course_service
SPRING_DATASOURCE_URL: jdbc:postgresql://database:5432/course_service
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
dapr-course:
Expand All @@ -52,7 +51,7 @@ services:
expose:
- "6379"
volumes:
coursedata:
dbdata:
testdata:
networks:
default:
Expand Down
20 changes: 20 additions & 0 deletions pg-init-scripts/create-multiple-databases.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

# Script taken with modifications from https://dev.to/bgord/multiple-postgres-databases-in-a-single-docker-container-417l

set -e
set -u

function create_user_and_database() {
local database=$1
echo " Creating user and database '$database'"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
CREATE DATABASE "$database";
GRANT ALL PRIVILEGES ON DATABASE "$database" TO "$POSTGRES_USER";
EOSQL
}

for var in $(env | grep '^POSTGRES_CREATE_DB_' | awk -F '=' '{print $2}'); do
echo "Creating database: $var"
create_user_and_database $var
done
Loading