0

I'm starting to use Docker and I have some questions about build a project using nodejs, sequelize-cli and postgres.

How can I do to exec npx sequelize-cli db:migrate inside the container?

My Dockerfile:

FROM node:alpine
 
WORKDIR /app
 
COPY package*.json ./
 
RUN npm install
 
COPY . .
 
EXPOSE 3000
 
CMD ["npm", "run", "dev"]

And my docker-compose:

    version: '3'
    volumes:
      data:
    services:
      db:
        image: postgres:9.6
        env_file: .env
        volumes:
          - data:/var/lib/postgres/data
        environment:
          - POSTGRES_USER=$DB_USE
          - POSTGRES_PASSWORD=$DB_PASSWORD
          - POSTGRES_DB=$DB_NAME
      app:
        build:
          context: .
          dockerfile: Dockerfile
        volumes: 
          - /app/node_modules
          - .:/app

Thanks all.

Lucas Alvine
  • 1
  • 1
  • 1

3 Answers3

4

you need to add an entrypoint.sh to run npm commands, something like this:

# docker-compose.yml

version: '3'
services:
  app:
    entrypoint: ["/bin/bash", "./entrypoint.sh"]
    image: node:10.12.0
    ports:
      - "3000:3000"

and with sequelize-cli in your package.json:

# entrypoint.sh

npm install
npx sequelize-cli db:migrate
Alireza Zarei
  • 314
  • 1
  • 4
  • 10
2

just run docker-compose run --rm app npx sequelize-cli db:migrate

or... if you have the service already running, you could simply login to the container (docker-compose exec app bash) and run the command inside your running container (npx sequelize-cli db:migrate)

Sander Garretsen
  • 1,683
  • 10
  • 19
0

In your Dockerfile you can add:

RUN npm install -g sequelize-cli

and then docker exec app sequelize db:migrate --env production

Marcin
  • 427
  • 1
  • 6
  • 17