Skip to content

Commit 7d861e4

Browse files
committed
Implement seperate entry point script for docker compose to prevent race conditions between migration application and server db usage
1 parent 0a32543 commit 7d861e4

File tree

3 files changed

+16
-17
lines changed

3 files changed

+16
-17
lines changed

backend/Dockerfile

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ COPY requirements.txt .
88
RUN pip install --upgrade pip && pip install --no-cache-dir -r requirements.txt
99

1010
# copy app source code
11-
COPY app ./app
12-
COPY alembic.ini ./alembic.ini
13-
COPY alembic ./alembic
11+
COPY . .
12+
13+
RUN chmod +x entrypoint.sh
1414

1515
# environment variables
1616
ENV PYTHONUNBUFFERED=1
1717

1818
# entrypoint: run migrations then start up
19-
CMD ["sh", "-c", "alembic upgrade head && uvicorn app.main:app --host 0.0.0.0 --port 8000"]
19+
ENTRYPOINT ["./entrypoint.sh"]
2020

2121

backend/entrypoint.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/sh
2+
set -e
3+
4+
echo "Waiting for database and running migrations..."
5+
6+
until alembic upgrade head; do
7+
echo "Migration failed, retrying in 2 seconds..."
8+
sleep 2
9+
done
10+
11+
echo "Migrations complete. Starting server..."
12+
exec uvicorn app.main:app --host 0.0.0.0 --port 8000

docker-compose.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,6 @@ services:
2828
interval: 5s
2929
timeout: 5s
3030
retries: 5
31-
32-
migrate:
33-
image: eventrelay-api:latest
34-
command: alembic upgrade head
35-
environment:
36-
POSTGRES_USER: eventrelayuser
37-
POSTGRES_PASSWORD: 123
38-
POSTGRES_DB: eventrelay
39-
POSTGRES_HOST: db
40-
POSTGRES_PORT: 5432
41-
depends_on:
42-
db:
43-
condition: service_healthy
4431

4532
volumes:
4633
postgres_data:

0 commit comments

Comments
 (0)