Search code examples
postgresqldockerdocker-composedatabase-migration

Apply Migration Scripts on docker startup


I am Unable to initialise database with schema evolution manager on docker startup. My docker file is as follows

FROM postgres:10-alpine

WORKDIR /opt/ext-api-db
COPY . .

RUN apk add py-pip ruby ruby-rdoc
RUN gem install schema-evolution-manager && \
    pip install awscli

RUN cp docker-entrypoint-initdb.d/* /docker-entrypoint-initdb.d

EXPOSE 5432

docker-entrypoint-initdb.d contain one init script

set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
    CREATE DATABASE ${POSTGRES_USER}_test;
    GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_USER}_test TO $POSTGRES_USER;
EOSQL

# Applying Sem Scripts
echo
echo 'PostgreSQL initialzing Data...'
echo

"pg_ctl -o" -c listen_addresses='localhost'" -D "$PGDATA" -w restart"

sem-apply --url postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost/$POSTGRES_DB
sem-apply --url postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost/$POSTGRES_DB_test

On docker container startup the database is created fine but the schema evolution manager says its unable to connect to database Here is the complete stack trace

/usr/local/bin/docker-entrypoint.sh: running /docker-entrypoint-initdb.d/init.sh
CREATE DATABASE
GRANT

PostgreSQL initialzing Data...

psql: could not connect to server: Connection refused
    Is the server running on host "localhost" (127.0.0.1) and accepting
    TCP/IP connections on port 5432?
could not connect to server: Address not available
    Is the server running on host "localhost" (::1) and accepting
    TCP/IP connections on port 5432?
/usr/lib/ruby/gems/2.5.0/gems/schema-evolution-manager-0.9.39/lib/schema-evolution-manager/library.rb:136:in `rescue in system_or_error': Error running command[psql --no-align --tuples-only --no-psqlrc --command "select count(*) from pg_namespace where nspname='schema_evolution_manager'" postgresql://user:pass@localhost/db]: Non zero exit code[pid 47 exit 2] running command[psql --no-align --tuples-only --no-psqlrc --command "select count(*) from pg_namespace where nspname='schema_evolution_manager'" postgresql://user:pass@localhost/db] (RuntimeError)
    from /usr/lib/ruby/gems/2.5.0/gems/schema-evolution-manager-0.9.39/lib/schema-evolution-manager/library.rb:129:in `system_or_error'
    from /usr/lib/ruby/gems/2.5.0/gems/schema-evolution-manager-0.9.39/lib/schema-evolution-manager/db.rb:32:in `psql_command'
    from /usr/lib/ruby/gems/2.5.0/gems/schema-evolution-manager-0.9.39/lib/schema-evolution-manager/db.rb:90:in `schema_schema_evolution_manager_exists?'
    from /usr/lib/ruby/gems/2.5.0/gems/schema-evolution-manager-0.9.39/lib/schema-evolution-manager/scripts.rb:86:in `scripts_previously_run'
    from /usr/lib/ruby/gems/2.5.0/gems/schema-evolution-manager-0.9.39/lib/schema-evolution-manager/scripts.rb:48:in `each_pending'
    from /usr/lib/ruby/gems/2.5.0/gems/schema-evolution-manager-0.9.39/lib/schema-evolution-manager/db.rb:22:in `bootstrap!'
    from /usr/lib/ruby/gems/2.5.0/gems/schema-evolution-manager-0.9.39/bin/sem-apply:30:in `<top (required)>'
    from /usr/bin/sem-apply:23:in `load'
    from /usr/bin/sem-apply:23:in `<main>'

Here is my docker-compose config

version: "3"
services:
  ext-api-db:
    build:
      context: .
    container_name: ext-api-db
    env_file: docker-compose.env
    hostname: ext-api-db
    networks:
      - ext
    ports:
      - "15432:5432"
    restart: unless-stopped
    volumes:
      - .:/opt/ext-api-db:delegated
networks:
  ext:
    external:
      name: ext

Can someone tell me how can i fix this issue?


Solution

  • Your entrypoint script should be:

    set -e
    
    psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
        CREATE DATABASE ${POSTGRES_USER}_test;
        GRANT ALL PRIVILEGES ON DATABASE ${POSTGRES_USER}_test TO $POSTGRES_USER;
    EOSQL
    
    # Applying Sem Scripts
    echo
    echo 'PostgreSQL initialzing Data...'
    echo
    
    pg_ctl -o "-c listen_addresses='localhost'" -D "$PGDATA" -w restart
    
    echo Server restarted with TCP/IP enabled
    
    sem-apply --url postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost/$POSTGRES_DB
    sem-apply --url postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost/$POSTGRES_DB_test