How do I run Prisma migrations in a Dockerized GraphQL + Postgres setup?

I’m new to using Prisma as well as Dockerizing my setup. I would like to specify my data model using Prisma, have Postgres as my database and use that in a GraphQL API (my current API uses apollo-server-express) that also deals with authentication and roles etc.

What I have now is a simple docker-compose.yml and a Dockerfile of my GraphQL API:

docker-compose.yml

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

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"]

How would I go about using Prisma and Postgres in this setup, where the migrations happen in some containerized way, instead of me executing a Prisma command manually in the CLI?

Pointing out my misconceptions, hints or feedback is appreciated! Thank you

Late answer to my own question: Like @Athir suggested I now separated the two processes and created two docker-compose.yml files: one named docker-compose.migrate.yml that’s responsible for running the migration and one named docker-compose.yml which is the main application.

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

With the following 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 (rn) 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

EDIT: example .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}

4

You need to run the migration before you start your nextjs app. There are several ways you can do this. Some people do this as part of this CI/CD scripts. In your case of using docker compose, you can change the startup command to run a script that runs the migration before starting up your app.

First, create your bash script

npx prisma migrate deploy
npm start

then change your Dockerfile to run the script

CMD ["startup.sh"]

3

Your solution is a bit chaotic. A simple one is probably this solution although it uses Nest.

And on a similar note, I have a solution with Drizzle instead of Prisma but you just have to replace one line.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật