Skip to content

Commit c11b4c0

Browse files
committed
feat: use single container for all postgresql databases
- now all the services may use the same port 5432 and don't have to be additionally configured - docker compose became lighter as we use one container for 3 databases instead of 3 containers
1 parent d3c5bf3 commit c11b4c0

File tree

7 files changed

+29
-27
lines changed

7 files changed

+29
-27
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ Local setup should work good on **Linux** and **macOS**. But **Windows** is not
5454
| Service | Name | Port |
5555
|----------|:-----:|:----:|
5656
| PostgreSQL | db | 5432 |
57-
| PostgreSQL (for tests) | db_test | 5432 |
5857
| Elasticsearch | esearch | 9200 |
5958
| RabbitMQ | queue | 5672, 15672 |
6059
| Mock Service (not in use) | jsonserver | 3001 |
@@ -105,7 +104,6 @@ Local setup should work good on **Linux** and **macOS**. But **Windows** is not
105104
| Service | Name | Port |
106105
|----------|:-----:|:----:|
107106
| PostgreSQL | db | 5432 |
108-
| PostgreSQL (for tests) | db_test | 5432 |
109107
| Elasticsearch | esearch | 9200 |
110108
| RabbitMQ | queue | 5672, 15672 |
111109
| Mock Service (not in use) | jsonserver | 3001 |

config/test.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
"rabbitmqUrl": "amqp://localhost:5672",
1818
"connectProjectsUrl": "https://local.topcoder-dev.com/projects/",
1919
"dbConfig": {
20-
"masterUrl": "postgres://coder:mysecretpassword@localhost:5433/projectsdb_test",
20+
"masterUrl": "postgres://coder:mysecretpassword@localhost:5432/projectsdb_test",
2121
"maxPoolSize": 50,
2222
"minPoolSize": 4,
2323
"idleTimeout": 1000

local/docker-compose.yml

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,13 @@ services:
55
ports:
66
- "3001:3001"
77
db:
8-
image: "postgres:9.5"
8+
build: "postgres-db"
99
ports:
1010
- "5432:5432"
1111
environment:
1212
- POSTGRES_PASSWORD=mysecretpassword
1313
- POSTGRES_USER=coder
14-
- POSTGRES_DB=projectsdb
15-
db_test:
16-
image: "postgres:9.5"
17-
ports:
18-
- "5433:5432"
19-
environment:
20-
- POSTGRES_PASSWORD=mysecretpassword
21-
- POSTGRES_USER=coder
22-
- POSTGRES_DB=projectsdb_test
14+
- POSTGRES_MULTIPLE_DATABASES=projectsdb,projectsdb_test
2315
esearch:
2416
image: "elasticsearch:2.3"
2517
ports:

local/full/docker-compose.base.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ services:
1515
- TC_API_V3_BASE_URL=https://api.topcoder-dev.com/v3
1616
- KAFKA_URL=kafka:9093
1717
- AUTH_SECRET=secret
18-
- DATABASE_URL=postgresql://coder:mysecretpassword@notifications_db:5432/tc_notifications
18+
- DATABASE_URL=postgresql://coder:mysecretpassword@db:5432/tc_notifications
1919
- JWKS_URI=test
2020
- LOG_LEVEL=debug
2121
- ENV=development

local/full/docker-compose.yml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,8 @@ services:
88
extends:
99
file: ../docker-compose.yml
1010
service: db
11-
notifications_db:
12-
image: "postgres:9.5"
13-
expose:
14-
- "5432"
1511
environment:
16-
- POSTGRES_PASSWORD=mysecretpassword
17-
- POSTGRES_USER=coder
18-
- POSTGRES_DB=tc_notifications
19-
db_test:
20-
extends:
21-
file: ../docker-compose.yml
22-
service: db_test
12+
- POSTGRES_MULTIPLE_DATABASES=projectsdb,projectsdb_test,tc_notifications
2313
esearch:
2414
extends:
2515
file: ../docker-compose.yml
@@ -101,8 +91,7 @@ services:
10191
ports:
10292
- "4002:4002"
10393
depends_on:
104-
- notifications_db
105-
- esearch
94+
- db
10695
environment:
10796
- PORT=4002
10897
tc-notifications-processor:

local/postgres-db/Dockerfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
FROM postgres:9.5
2+
COPY create-multiple-postgresql-databases.sh /docker-entrypoint-initdb.d/
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/bin/bash
2+
3+
set -e
4+
set -u
5+
6+
function create_user_and_database() {
7+
local database=$1
8+
echo " Creating database '$database'"
9+
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" <<-EOSQL
10+
CREATE DATABASE $database;
11+
GRANT ALL PRIVILEGES ON DATABASE $database TO $POSTGRES_USER;
12+
EOSQL
13+
}
14+
15+
if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then
16+
echo "Multiple database creation requested: $POSTGRES_MULTIPLE_DATABASES"
17+
for db in $(echo $POSTGRES_MULTIPLE_DATABASES | tr ',' ' '); do
18+
create_user_and_database $db
19+
done
20+
echo "Multiple databases created"
21+
fi

0 commit comments

Comments
 (0)