对我自己的问题的迟到回答:就像 @Athir 建议的那样,我现在将两个进程分开并创建了两个 docker-compose.yml 文件:一个名为docker-compose.migrate.yml
负责运行迁移的一个名为docker-compose.yml
这是主要的应用程序。
My docker-compose.migrate.yml
:
version: '3'
services:
prisma-migrate:
container_name: prisma-migrate
build: ./api/prisma
env_file:
- .env
environment:
DB_HOST: <secret>
depends_on:
- db
db:
image: postgres:13
container_name: db
restart: always
env_file:
- .env
environment:
DB_PORT: 5432
ports:
- ${DB_PORT}:5432
volumes:
- ${POSTGRES_VOLUME_DIR}:/var/lib/postgresql/data
使用以下 Prisma Dockerfile:
FROM node:14
RUN echo $DATABASE_URL
WORKDIR /app
COPY ./package.json ./
COPY . ./prisma/
RUN chmod +x ./prisma/wait-for-postgres.sh
RUN npm install
RUN npx prisma generate
RUN apt update
RUN apt --assume-yes install postgresql-client
# Git will replace the LF line-endings with CRLF, causing issues while executing the wait-for-postgres shell script
# Install dos2unix and replace CRLF (\r\n) newlines with LF (\n)
RUN apt --assume-yes install dos2unix
RUN dos2unix ./prisma/wait-for-postgres.sh
CMD sh ./prisma/wait-for-postgres.sh ${DB_HOST} ${POSTGRES_USER} npx prisma migrate deploy && npx prisma db seed --preview-feature
EDIT: wait-for-postgres.sh
:
#!/bin/sh
# wait-for-postgres.sh
set -e
host="$1"
user="$2"
shift
shift
cmd="$@"
until PGPASSWORD=$POSTGRES_PASSWORD psql -h "$host" -U "$user" -c '\q'; do
>&2 echo "Postgres is unavailable - sleeping"
sleep 1
done
>&2 echo "Postgres is up - executing command"
exec $cmd
编辑:示例.env
file:
# ---- DB ----
DB_HOST=localhost
DB_PORT=5432
DB_SCHEMA=example
POSTGRES_DB=example
POSTGRES_USER=example
POSTGRES_VOLUME_DIR=/path/where/you/want/to/store
POSTGRES_PASSWORD=example
DATABASE_URL=postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${DB_HOST}:${DB_PORT}/${POSTGRES_DB}?schema=${DB_SCHEMA}