2

Here's my docker-compose

version: '2'
services:
  weather-cities:
    build:
      context: .
    volumes:
      - .:/usr/app
      - /usr/app/node_modules/
    ports:
      - "8080:8080"

    # Set environment variables from this file
    # env_file:
    #   - .env

    # Overwrite any env var defined in .env file (if required)
    environment:
      - DB_NAME=test
      - DB_PORT=5432
      - DB_HOST=postgres
      - DB_USERNAME=test
      - DB_PASSWORD=challenge
      - APP_PORT=8080

    links:
      - postgres

  postgres:
    image: "postgres:9.4.11"
    ports:
      - "5432:5432"
    environment:
      - POSTGRES_USER=test
      - POSTGRES_PASSWORD=challenge

Running this migration script

"db:migrate": "./node_modules/.bin/sequelize --migrations-path=migrations --models-path=models --config=config/config.js db:migrate"

And here's my Dockerfile

FROM node:8.10.0

WORKDIR /usr/app

COPY package.json .
RUN npm install --quiet

COPY . .

RUN npm run db:migrate
RUN npm run db:seed

EXPOSE 8080

CMD ["npm", "start"]

Output:

Loaded configuration file "config/config.js". Using environment "development".

ERROR: connect ECONNREFUSED 127.0.0.1:5432

config.js

let config = {
  "development": {
    "username": process.env.DB_USERNAME,
    "password": process.env.DB_PASSWORD,
    "database": process.env.DB_NAME,
    "host": process.env.DB_HOST,
    "dialect": "postgres",
    "port": process.env.DB_PORT
  }
};

module.exports = config;
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
nirvair
  • 4,001
  • 10
  • 51
  • 85

1 Answers1

1

When you run docker-compose up, two things happen behind the scene:

  1. docker-compose build - the images are either build from the specified Dockerfile and context, either pulled from docker images repository.
  2. docker-compose start - the containers are started out of the images for the services

During the build phase, none of your container are up and running yet.
This is what causes your actual issue, you expect, in your node build, the postgress container to be up, running and listening to the port 5432, which will never be true, because the container start phase will not happen before all the images are build.

What you are seeking to achieve can be done via entrypoint though.

β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83
  • Showing you how would actually be lengthy and would cause a lot of back and forth discussion, maybe try it and post a follow up question if you have any issue with it. – β.εηοιτ.βε Mar 26 '19 at 22:37
  • I think the environment variables are not set. When I do console.log in config.js, it prints undefined. And when I hard-code DB_HOST as "postgres", it shows this `ERROR: getaddrinfo ENOTFOUND postgres postgres:5432` – nirvair Mar 26 '19 at 22:51
  • Same reason. Your `ENV` are injected at `run` time of your containers, not a `build` time. (extra reference: https://github.com/docker/compose/issues/1837#issuecomment-129911287) – β.εηοιτ.βε Mar 26 '19 at 22:54
  • Ok. But, I didn't understand the use-case of entrypoint and how or where do I use it :/ – nirvair Mar 26 '19 at 22:54
  • Your starting point would be either https://stackoverflow.com/q/21553353/2123530 or https://medium.freecodecamp.org/docker-entrypoint-cmd-dockerfile-best-practices-abc591c30e21 – β.εηοιτ.βε Mar 26 '19 at 22:56
  • Do I need to do something like this - ENTRYPOINT ["npm" "db:migration"] ? – nirvair Mar 26 '19 at 23:05
  • 1
    Nope you will need to write a script as your entrypoint that would execute all commands that needs the postgress container to be up, but prior to that you should also check that this container is effectively listening to the port 5432. This could also be an inspiration for you: https://success.docker.com/article/use-a-script-to-initialize-stateful-container-data – β.εηοιτ.βε Mar 26 '19 at 23:14
  • 1
    You are totally deviating from your original question in here. Try your best and if still not, open a new question explaining your new issue – β.εηοιτ.βε Mar 26 '19 at 23:48
  • https://stackoverflow.com/questions/55368017/running-sequelize-migrations-in-docker-compose <- new issue – nirvair Mar 27 '19 at 00:19