0

Hi I am trying to adapt my-app to docker.

My app stack is

  • Ruby 2.4.0
  • Rails 5.0.1
  • Postgres 9.5.8

I created a Dockerfile to create an image of my-app

FROM ruby:2.4

RUN apt-get update && apt-get install -y \
    software-properties-common \
    python-software-properties \
    build-essential \
    libpq-dev \
    libxml2-dev libxslt1-dev \
    libqt4-webkit libqt4-dev xvfb \
    nodejs \
    postgresql

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY Gemfile /usr/src/app
COPY Gemfile.lock /usr/src/app
RUN bundle install
ADD . /usr/src/app
COPY ./docker/docker-entrypoint.sh /usr/src/app/docker-entrypoint.sh
RUN chmod +x /usr/src/app/docker-entrypoint.sh
RUN bundle install

Then I created a docker-compose.yml

version: '3'
services:

  app:
    image: app_rails
    entrypoint: ['/usr/src/app/docker-entrypoint.sh']
    ports:
      - "3000:3000"
    depends_on:
      - postgres
    links:
      - postgres
    volumes:
      - .:/usr/src/app
    environment:
      DATABASE_URL: postgres://postgres@postgres@postgres:5432/postgres?pool=5&encoding=utf-8

  postgres:
    image: postgres:9.5.8
    ports:
     - "5432:5432"
    environment:
      POSTGRES_USER: postgres
      POSTGRES_PASSWORD: postgres
      POSTGRES_DB: postgres

And here is the script docker-entrypoint.sh

#!/bin/sh
set -e

host="$1"
echo "host: ${host}"

if [ "$#" -gt 0 ]; then shift; fi

until psql -h "$host" -U "postgres" -c '\q'; do
  >&2 echo "Postgres is unavailable - sleeping"
  sleep 1
done

rails db:create
rails db:migrate RAILS_ENV=development

bundle exec rails server -b 0.0.0.0

exec "$@"

and my database.yml

# SQLite version 3.x
#   gem install sqlite3
#
#   Ensure the SQLite 3 gem is defined in your Gemfile
#   gem 'sqlite3'
#
default: &default
  adapter: postgresql
  pool: 5
  timeout: 5000

development:
  adapter: postgresql
  database: app_development
  pool: 5
  timeout: 5000
  url: postgres://postgres@postgres/postgres:5432

test:
  adapter: postgresql
  database: app_test
  pool: 5
  timeout: 5000

production:
  adapter: postgresql
  database: app_production
  pool: 5
  timeout: 5000

I am having trouble running the script

web_1       |   connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
web_1       | Postgres is unavailable - sleeping

Since I am new to docker an advise will be wellcome

2 Answers 2

2

You're DATABASE_URL can't be localhost, it needs to be the name of service. It also needs username and password in there:

postgres://postgres:postgres@postgres/postgres

It doesn't need post since it uses the default. Here's examples of URL's: https://stackoverflow.com/a/20722229/749924

Sign up to request clarification or add additional context in comments.

3 Comments

@Bret_Fisher thank you for your answer. It was very helpful to keep working. Sorry that I fully updated the question, but since your answer I was working in trying to make it work, but I am a little stuck. PS. I think your course in coursera is one of the best I took
I corrected my answer I had the : and @ backward :P
Tip: likely not related to your failure, but ideally you want your ENTRYPOINT to just do the wait and db migrate steps and then add a CMD to Dockerfile of bundle exec rails server -b 0.0.0.0 so it starts the server correctly.
1

Try

chmod +x /usr/src/my-app/docker-entrypoint.sh

to ensure your entry point is an executable

4 Comments

thank you for your answer I already execute that in the Dockerfile
you run docker-compose as sudo ? and make chmod +x docker-entrypoint.sh before building the image
thank you, now it changed the output (I did not executed with sudo) I wrote it as an update of the question
sorry that I fully update the question, but I was working, and I stuck, but now is different. Your answer helped me to get to another point.

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.