0

When I run the following Dockerfile,in the middle of the process, after installing prisma, the RUN command db-migrate up is stopped. But when I used docker exec bin bash to run it, it worked without any problem. I don't think I can run the app before serving it. But there is a workaround like putting the migration commands as a service in docker-compose.yml. How to achieve it? or if there's any way to run those RUN commands of migration in this Dockerfile?

Dockerfile

FROM node:16.15.0-alpine

WORKDIR /app

COPY package*.json ./

# generated prisma files
COPY prisma ./prisma/

# COPY ENV variable
COPY .env ./

# COPY
COPY . .

RUN npm install

RUN npm install -g db-migrate

RUN npm install -g prisma

RUN db-migrate up

RUN prisma db pull

RUN prisma generate

EXPOSE 3000

CMD ["npm", "run", "dev"]

docker-compose.yml

version: '3.8'

services:
  mysqldb:
    image: mysql:5.7
    restart: unless-stopped
    env_file: ./.env
    environment:
      - MYSQL_ROOT_PASSWORD=$MYSQLDB_ROOT_PASSWORD
      - MYSQL_DATABASE=$MYSQLDB_DATABASE
    ports:
      - $MYSQLDB_LOCAL_PORT:$MYSQLDB_DOCKER_PORT
    volumes:
      - db:/var/lib/mysql
  auth:
    depends_on:
      - mysqldb
    build: ./auth
    restart: unless-stopped
    env_file: ./.env
    ports:
      - $NODE_LOCAL_PORT:$NODE_DOCKER_PORT
    environment:
      - DB_HOST=mysqldb
      - DB_USER=$MYSQLDB_USER
      - DB_PASSWORD=$MYSQLDB_ROOT_PASSWORD
      - DB_NAME=$MYSQLDB_DATABASE
      - DB_PORT=$MYSQLDB_DOCKER_PORT
    stdin_open: true
    tty: true

volumes:
  db:
Ben
  • 395
  • 1
  • 5
  • 16
  • Your Dockerfile can't connect to the database at all. (If I were to `docker pull` your built image and run it on my system, my local database wouldn't have the migrations applied.) [How do you perform Django database migrations when using Docker-Compose?](https://stackoverflow.com/questions/33992867/how-do-you-perform-django-database-migrations-when-using-docker-compose) is a different technology stack but goes through several options. – David Maze Jul 25 '22 at 09:50
  • [How do I run Prisma migrations in a Dockerized GraphQL + Postgres setup?](https://stackoverflow.com/questions/66646432/how-do-i-run-prisma-migrations-in-a-dockerized-graphql-postgres-setup) is a little more Prisms-specific though not the specific tool you're using here. – David Maze Jul 25 '22 at 09:50

0 Answers0