0

Consider the Dockerfile :

Dockerfile :

FROM node:14
COPY . /app
RUN npm i
RUN npm run build_database_migrations <<<<====== This is the problematic command

... more commands 

Which uses sequelize-cli :

In package.json :

"scripts": {
   "build_database_migrations": "sequelize db:migrate", 
  }

And the Docker-compose :

Docker-compose.yaml

version: "3"
services:
  
  postgresql:
    image: postgres:latest
    environment:
      POSTGRES_USER: user1
      POSTGRES_PASSWORD: pass1
      POSTGRES_DB: somedb      
    ports:
      - "5432:5432"
 
  app:    
    build:
      context: ./employee
      dockerfile: ./Dockerfile
    ports:
      - "9000:9000"
    restart: on-failure
    depends_on:    
      - postgresql      
    links:
      - postgresql

Whenever I run the Docker-compose up the app never waits for postgresql to be up and running , which results in building the app container and running the command RUN npm run build_database_migrations before the Postgreqsql Container is listening on port 5432.

How can we force the app Container to wait until Postreqsql container is ready and listening on port 5432 , so it would be able to run the migrations on a live Postgresql Container ?

JAN
  • 21,236
  • 66
  • 181
  • 318
  • You could have a "prebuild_database_migrations" script that uses e.g. wait-on to wait for the port to be available. But I don't think the database would ever be available during the *build* phase, you should migrate at the start of the *run* phase. – jonrsharpe Jan 03 '21 at 17:53
  • @jonrsharpe: I have them , but I want to run them automatically once the DB container is ready.How do we do that ? – JAN Jan 03 '21 at 17:55
  • You have *what*? Again I don't think the DB will *ever* be available during the build phase defined in the Dockerfile. – jonrsharpe Jan 03 '21 at 17:57
  • I flagged this as a duplicate of the canonical question about one Compose-managed container waiting for another; but @jonrsharpe is right, regardless of that, your image build can _never_ talk to the database (among other things it will not be attached to the Compose network). – David Maze Jan 03 '21 at 18:13

0 Answers0