Skip to content

Commit

Permalink
Merge pull request #4 from MEITREX/docker
Browse files Browse the repository at this point in the history
Update docker compose to use unified db container
  • Loading branch information
v-morlock authored Sep 13, 2024
2 parents aab6b98 + 4130f24 commit 7fb3f9f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
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

0 comments on commit 7fb3f9f

Please sign in to comment.