From c6b00365fd0f0d7192ba9fd56ab14b97b91e8d27 Mon Sep 17 00:00:00 2001 From: Keith Stevens Date: Wed, 21 Dec 2022 12:17:08 +0900 Subject: [PATCH] Simplifying the end to end docker setup to ensure it prepares the web database properly before the website starts. --- docker/Dockerfile.website | 9 ++++++++ scripts/endtoend-demo/docker-compose.yaml | 1 + website/wait-for-postgres.sh | 25 +++++++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100755 website/wait-for-postgres.sh diff --git a/docker/Dockerfile.website b/docker/Dockerfile.website index cd723833..afb18baf 100644 --- a/docker/Dockerfile.website +++ b/docker/Dockerfile.website @@ -39,6 +39,15 @@ RUN adduser --system --uid 1001 nextjs COPY --from=builder /app/public ./public +# Copy over the prisma schema so we can to `npx prisma db push` and ensure the +# database exists on startup. +COPY --chown=nextjs:nodejs ./website/prisma/schema.prisma ./ +# Copy over a startup script that'll run `npx prisma db push` before starting +# the webserver. This ensures the webserver can actually check user accounts. +# This is a prisma variant of the postgres solution suggested in +# https://docs.docker.com/compose/startup-order/ +COPY --chown=nextjs:nodejs ./website/wait-for-postgres.sh ./ + # Automatically leverage output traces to reduce image size # https://nextjs.org/docs/advanced-features/output-file-tracing COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ diff --git a/scripts/endtoend-demo/docker-compose.yaml b/scripts/endtoend-demo/docker-compose.yaml index b17a7b74..8aa2b0fa 100644 --- a/scripts/endtoend-demo/docker-compose.yaml +++ b/scripts/endtoend-demo/docker-compose.yaml @@ -87,3 +87,4 @@ services: condition: service_healthy ports: - "3000:3000" + command: bash wait-for-postgres.sh node server.js diff --git a/website/wait-for-postgres.sh b/website/wait-for-postgres.sh new file mode 100755 index 00000000..6ff1a024 --- /dev/null +++ b/website/wait-for-postgres.sh @@ -0,0 +1,25 @@ +#!/bin/sh +# wait-for-postgres.sh +# +# This script tries to connect to a postgres database and then runs +# `npx prisma db push` +# It repeats until that setup completes (running multiple times will be a +# noop). Then, it'll run the next command provided. +# +# You don't need to provide the connection URL. This reads it from the +# DATABASE_URL environment variable which should be set. +# +# To run we suggest: +# ./wait-for-postgres.sh npm run dev + +set -e + +until npx prisma db push --skip-generate; do + >&2 echo "Postgres is unavailable - sleeping" + sleep 1 +done + +>&2 echo "Postgres is up - executing command" +# Print and execute all other arguments starting with `$1` +# So `exec "$1" "$2" "$3" ...` +exec "$@"