1

I have a simple Rails application that I'm trying to run using Docker. When I run docker-compose up it runs but when I load a page it tells me that my db doesn't exist. Even though when I run docker-compose run app rails db:create it says that the db already exists.

docker-compose.yml

version: '2'

services:
  db:
    image: postgres 
  app:
    build: ./rails
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - ./app:/app
    ports:
      - "3000:3000"
    depends_on:
      - db

./rails/Dockerfile

FROM ruby:2.3.3
RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /app
WORKDIR /app
ADD ./Gemfile /app/Gemfile
ADD ./Gemfile.lock /app/Gemfile.lock
RUN bundle install

I'm new to Docker so I may be missing something basic.

1
  • In my experience, the database usually takes some time to be usable. As an experiment, try wrapping your command in a shell script that sleeps 10 seconds before running your rails server command. Commented Dec 10, 2016 at 18:44

1 Answer 1

0

Rickkwa is correct, db takes some time to get stable.

You could potentially work around the issue by having a Ruby script with the following:

begin
  retries ||= 0
  Mysql2::Client.new(host: 'db', username: 'root')
rescue
  sleep 1
  retry if (retries += 1) < 10
end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.