title: How to create postgres database and run migration when docker-compose up
body: <p>I'm setting up a simple backend that perform CRUD action with postgres database and want to create database and migration automatically when the docker-compose up runs.</p>
  
  <p>I have already tried to add below code to <em>Dockerfile</em> or <em>entrypoint.sh</em> but none of them work.</p>
  
  <pre><code>createdb --host=localhost -p 5432 --username=postgres --no-password pg_development
  createdb db:migrate
  </code></pre>
  
  <p>This code will work if run separately after docker is fully up</p>
  
  <p>I have already tried to add <code>- ./db-init:/docker-entrypoint-initdb.d</code> to the volumes but that didn't work either</p>
  
  <p>This is the <em>Dockerfile</em></p>
  
  <pre><code>FROM node:10.12.0
  
  # Create app directory
  RUN mkdir -p /restify-pg
  WORKDIR /restify-pg
  
  EXPOSE 1337
  
  ENTRYPOINT [ "./entrypoint.sh" ]
  </code></pre>
  
  <p>This is my <em>docker-compose.yml</em></p>
  
  <pre><code>version: '3'
  services:
    db:
      image: "postgres:11.2"
      ports:
        - "5432:5432"
      volumes:
        - ./pgData:/var/lib/psotgresql/data
      environment:
        POSTGRES_USER: postgres
        POSTGRES_PASSWORD:
        POSTGRES_DB: pg_development
  
    app:
      build:
        context: .
        dockerfile: Dockerfile
      ports:
        - "3000:3000"
      volumes:
        - .:/restify-pg
      environment:
        DB_HOST: db
  </code></pre>
  
  <p><em>entrypoint.sh</em> (in here I get <code>createdb: command not found</code>)</p>
  
  <pre><code>#!/bin/bash
  
  cd app
  
  createdb --host=localhost -p 5432 --username=postgres --no-password pg_development
  sequelize db:migrate
  
  npm install
  npm run dev
  </code></pre>
  
  <p>I expect that when I run docker, the migration and the db creation would happen.</p>