1

I am new to docker rails and am attempting to implement an existing rails project in docker containers following the instructions here: https://hackernoon.com/dockerizing-an-existing-rails-postgresql-app-with-docker-compose-a30a7e1b3f40

docker-compose.yml

version: '3'
services:
 web:
  build: .
  command: bundle exec rails s -p 3000 -b '0.0.0.0'
  volumes:
   - .:/myapp
  ports:
   - "3000:3000"
  depends_on:
   - db
 db:
  image: postgres
  volumes:
   - ./tmp/db:/var/lib/postgresql/data

Database.yml

default: &default
 adapter: postgresql
 encoding: unicode
 host: db
 username: postgres
 pool: 5
development:
 <<: *default
 database: myapp_development

Dockerfile

FROM ruby:2.5.3
RUN apt-get update && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp

All the containers are created and spin up just fine. I can see this from the console output after running docker-compose up. I can even connect to the rails server. However, the rails server cannot connect to the postgres server:

enter image description here

Is there something I am missing in my docker configuration or do I need to make a change in my rails configuration for docker?

2
  • I think you need ports 5432:5432 under db in docker-compose Commented May 14, 2019 at 15:23
  • What does docker-compose logs db show? It might be that your DB container doesn't even start in the first place because you populate the /var/lib/postgresql/data folder, which can lead to problems. Commented May 14, 2019 at 16:29

1 Answer 1

1

That error looks like it's not forwarding the port for postgres try changing docker-compose to forward the port

I don't have a ton of experience with Docker but I think you're going to find that your db is not persisted when you restart the docker containers. I would suggest using an external volume for the db

Your Dockerfile looks fine

If someone with more experience see this please confirm in the comments

docker-compose.yml

version: '3'
services:
 web:
  build: .
  command: bundle exec rails s -p 3000 -b '0.0.0.0'
  volumes:
   - .:/myapp
  ports:
   - "3000:3000"
  depends_on:
   - db
 db:
  image: postgres
  volumes:
   - pg-db:/var/lib/postgresql/data
   - ../db:/db
  ports:
   - "5432:5432"
 volumes:
   pg-db:
     external: true

Database.yml

default: &default
 adapter: postgresql
 encoding: unicode
 host: db
 username: postgres
 pool: 5
development:
 <<: *default
 database: myapp_development

Dockerfile

FROM ruby:2.5.3
RUN apt-get update && apt-get install -y build-essential libpq-dev nodejs
RUN mkdir /myapp
WORKDIR /myapp
COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock
RUN bundle install
COPY . /myapp
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response. After making your changes I can see the postgres service using pgadmin. However, my rails app is still having issues seeing the database. Would this indicate in issue with the rails configuration? This app is successfully implemented on a co-worker's dev environment.

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.