Problem waiting for postgres with dockerize


#1

I am trying to set up a rails app to run with postgres on circle 2.0. Sometimes the tests run successfully but other times they fail, with the error that the connection to the postgres database was refused. I believe that I’m experiencing the race-condition described here, so followed the recommended solution and added dockerize to prevent it:

version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
       - image: circleci/ruby:2.4.1-node-browsers
         environment:
           - RAILS_ENV=test
           - DATABASE_URL=postgres://xxx@localhost:5432/yyy
       - image: circleci/postgres:9.6.2
         environment:
           - POSTGRES_USER=xxx
           - POSTGRES_DB=yyy

    working_directory: ~/repo

    steps:
      - checkout

      # omitted steps ...

      - run:
          name: Wait for db
          command: |
            dockerize -wait tcp://localhost:5432 -timeout 1m

      # Database setup
      - run:
          name: Set up DB
          command: |
            bundle exec rake db:create db:schema:load --trace
            bundle exec rake db:migrate

This didn’t seem to have any effect and I’m still experiencing the error:

Can anyone help me figure out what’s going on here?


#2

I was able to get mine to work. Your dockerize wait step is the same as mine, but I have more env vars when setting up the images:

      - image: circleci/ruby:2.4.1-node-browsers
        environment:
          PGHOST: localhost
          PGUSER: xxx
          DATABASE_URL: postgres://xxx@localhost:5432/xxx_test
      - image: circleci/postgres:9.6.2-alpine
        environment:
          POSTGRES_USER: xxx
          POSTGRES_DB: xxx_test
          POSTGRES_PASSWORD: ""

Other than that, we’re pretty much identical


#4

Thanks rob!

For anyone who’s interested, this issue went away of its own accord - possibly because as I added more gems to the rails app those steps became slower …

I still don’t see how the above behaviour is possible if dockerize is behaving correctly, especially given that I had successfully connected to the db before with the same credentials.


#5

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.