Skip to content

Commit

Permalink
Update docker compose to use unified db container
Browse files Browse the repository at this point in the history
  • Loading branch information
myluki2000 committed Sep 13, 2024
1 parent d5d5967 commit 4fa7d23
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
20 changes: 10 additions & 10 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
version: "3"
services:
database-flashcard:
image: postgres:alpine
database:
image: pgvector/pgvector:pg16
restart: always
expose:
- 6032
- 5432
ports:
- "6032:5432"
- "5432:5432"
volumes:
- flashcarddata:/var/lib/postgresql/data
- dbdata:/var/lib/postgresql/data
- ./../flashcard_service/pg-init-scripts:/docker-entrypoint-initdb.d
environment:
- POSTGRES_USER=root
- POSTGRES_PASSWORD=root
- POSTGRES_DB=flashcard_service
- POSTGRES_CREATE_DB_FLASHCARD_SERVICE=flashcard_service
app-flashcard:
build:
context: ./../flashcard_service # necessary because otherwise docker-compose overrides the context path when merging multiple docker-compose.yml files
Expand All @@ -22,9 +22,9 @@ services:
- "6000:6000"
- "6001:6001"
depends_on:
- database-flashcard
- database
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://database-flashcard:5432/flashcard_service
SPRING_DATASOURCE_URL: jdbc:postgresql://database:5432/flashcard_service
SPRING_DATASOURCE_USERNAME: root
SPRING_DATASOURCE_PASSWORD: root
dapr-flashcard:
Expand All @@ -47,7 +47,7 @@ services:
expose:
- "6379"
volumes:
flashcarddata:
dbdata:
testdata:

networks:
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

0 comments on commit 4fa7d23

Please sign in to comment.