0

I'm using docker-compose and a yml file to start up a container. There are two scripts in the package.json I want to call:

"dev-start": "nodemon src/index.js",
"dev-migrate": "db-migrate --migrations-dir src/migrations --config src/database.json up"

For calling one I use command:script name, how can I call more than one script?

Omiron
  • 341
  • 3
  • 15

2 Answers2

0

If you want to launch a migration while the application is running, use docker exec -it name-of-container bash to open a shell in the running container and execute the command.

If you need to run the migration before the first launch of the application, run docker-compose run name-of-service bash, run the command that initialize the database, exit and launch docker-compose normally.

If you want to run the migrations before each launch of the application, you could write a shell script that performs both actions (migration, then launch the application) copy it in the image and call it as your default command.


For the 2 first paragraphs, you could also directly launch the migration command instead of running bash

Alexis N-o
  • 3,954
  • 26
  • 34
0

In my opinion it's the better practice to divide your migrations and server. I always do it like I have two services one for running my server and one for migrating data into my database And if you think it makes sense to separate them as they are doing two different jobs.

services:
migrate:
   build: .
   command: db-migrate --migrations-dir src/migrations --config src/database.json up
   depends_on:
     - db
   env_file:
     - .env
web:
   build: .
   command: nodemon src/index.js
   ports:
     - 8000:8000
   env_file:
     - .env
   depends_on:
     - db
     - migrate
 db:
   image: postgres:12.0-alpine
   volumes:
     - postgres_data:/var/lib/postgresql/data/
ArminMz
  • 325
  • 6
  • 9