如何在 Dockerized GraphQL + Postgres 设置中运行 Prisma 迁移?

Max*_*ger 5 postgresql docker dockerfile docker-compose prisma

我刚开始使用 Prisma 以及 Docker 化我的设置。我想使用 Prisma 指定我的数据模型,将 Postgres 作为我的数据库,并在 GraphQL API(我当前的 API 使用apollo-server-express)中使用它,该 API 还处理身份验证和角色等。

我现在拥有的是一个简单docker-compose.ymlDockerfileGraphQL API:

docker-compose.yml

services:
  api:
    build: ./api
    env_file:
      - .env
    volumes:
      - ./api:/usr/src/app
    ports:
      - ${API_PORT}:${API_PORT}
    command: npm start

Run Code Online (Sandbox Code Playgroud)

Dockerfile

# Latest LTS version
FROM node:14

# Set default values for environment variables
ENV API_PORT=3001

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./
RUN npm install

# Bundle app source
COPY . .

# Bind port
EXPOSE ${API_PORT}

# Start server
CMD ["npm", "start"]
Run Code Online (Sandbox Code Playgroud)

我将如何在此设置中使用 Prisma 和 Postgres,其中迁移以某种容器化方式进行,而不是在 CLI 中手动执行 Prisma 命令?

感谢指出我的误解、提示或反馈!谢谢

Max*_*ger 7

对我自己的问题的迟到回答:就像 @Athir 建议我现在分离两个进程并创建两个 docker-compose.yml 文件:一个名为docker-compose.migrate.yml负责运行迁移,另一个名docker-compose.yml为主应用程序。

我的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
Run Code Online (Sandbox Code Playgroud)

使用以下 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
Run Code Online (Sandbox Code Playgroud)

编辑:wait-for-postgres.sh

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
Run Code Online (Sandbox Code Playgroud)

编辑:示例.env文件:

# ---- 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}
Run Code Online (Sandbox Code Playgroud)