Skip to content

Commit 30db3e9

Browse files
committed
fix: Fix database initialisation in docker compose for sqlite and postgres
1 parent 428996b commit 30db3e9

File tree

7 files changed

+29
-7
lines changed

7 files changed

+29
-7
lines changed

examples/docker_compose/Dockerfile-DBInit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ RUN python3 -m venv /venv
1717
# Activate venv and install the package
1818
RUN /venv/bin/pip install .
1919

20-
CMD ["sh", "-c", "/app/init-db.sh"]
20+
CMD ["sh", "-c", "/app/init-db-postgres.sh"]

examples/docker_compose/Dockerfile-Hub

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ RUN pip install grader_labextension
1414

1515
RUN python3 -m pip install dockerspawner oauthenticator
1616

17-
CMD ["jupyterhub", "-f", "/app/jupyterhub_config.py"]
17+
CMD ["jupyterhub", "-f", "/app/jupyterhub_config.py"]

examples/docker_compose/Dockerfile-Service

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ FROM python:3.13-slim
22

33
# Install git
44
RUN apt-get update && \
5-
apt-get install -y git && \
5+
apt-get install -y git sqlite3 && \
66
apt-get clean && \
77
rm -rf /var/lib/apt/lists/*
88

examples/docker_compose/docker-compose-postgres.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
services:
2+
3+
service:
4+
command: [] # Use the default CMD from Dockerfile-Service, i.e. just apply migrations
5+
26
db-init:
37
build:
48
context: ../..
@@ -9,7 +13,7 @@ services:
913
volumes:
1014
- ./grader_service_config.py:/app/grader_service_config.py
1115
- ./data_only.sql:/app/data_only.sql
12-
- ./init-db.sh:/app/init-db.sh
16+
- ./init-db-postgres.sh:/app/init-db-postgres.sh
1317
depends_on:
1418
postgres:
1519
condition: service_healthy

examples/docker_compose/docker-compose.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ services:
55
build:
66
context: ../..
77
dockerfile: examples/docker_compose/Dockerfile-Service
8+
command: bash -c "
9+
/app/init-db.sh
10+
&& grader-service -f /app/grader_service_config.py
11+
"
812
volumes:
913
- ./grader_service_config.py:/app/grader_service_config.py
1014
- service_dir:/app/service_dir
15+
- ./init-db.sh:/app/init-db.sh
16+
- ./data_only.sql:/app/data_only.sql
1117
environment:
1218
DATABASE_TYPE: ${DATABASE_TYPE:-sqlite}
1319
RABBITMQ_GRADER_SERVICE_USERNAME: grader_user
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/usr/bin/env bash
2+
# Initialize a PostgreSQL database: run migrations and load initial data.
3+
set -e
4+
5+
/venv/bin/grader-service-migrate -f /app/grader_service_config.py
6+
# Try to load the initial data. This will fail on the first error (e.g. if the data already exists)
7+
# and roll back the transaction.
8+
psql -v ON_ERROR_STOP=1 -h postgres -U postgres -d grader --single-transaction -f /app/data_only.sql

examples/docker_compose/init-db.sh

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
#!/usr/bin/env bash
2+
# Initialize an sqlite database: run migrations and load initial data.
3+
set -e
14

2-
/venv/bin/grader-service-migrate -f /app/grader_service_config.py
3-
4-
psql -h postgres -U postgres -d grader -f /app/data_only.sql
5+
grader-service-migrate -f /app/grader_service_config.py
6+
# Insert some initial data into the database. The import will fail if the data already
7+
# has been inserted, so we ignore errors and just continue.
8+
sqlite3 /app/service_dir/grader.db < /app/data_only.sql || true

0 commit comments

Comments
 (0)