Can't start rails application via docker

Viewed 1615

I tried to build Docker container with Ruby on Rails application and can't do this.

Dockerfile

   FROM ruby:2.5.1
   RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

   RUN mkdir /noteapp
   WORKDIR /noteapp

   ADD Gemfile /noteapp/Gemfile
   ADD Gemfile.lock /noteapp/Gemfile.lock

   RUN bundle install

   ADD . /noteapp
   CMD ["rails","server","-b","0.0.0.0"]

docker-compose.yml

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

After running command

sudo docker-compose up --build

I got the result, noteapp_web_1 exited with code 0 After this action I tried to up my docker container and everything was okey, but my application not responding on localhost:3000

When i tried to saw logs(sudo docker logs id), i got the result console_result

But when I tried to connect to my container, I can't do this, and got a error:

sudo docker attach ee43805fd6cf
You cannot attach to a stopped container, start it first

What should i do to run my application?

Update, i used the solution of @Upendra Chahar And got this problem rails_error

rake_error

After that I fixed this problem and now i'm having problem with postgresql db: postgresql_db

3 Answers

First, you need to run this command for creating rails project inside in your container.

docker-compose run web rails new . --force --database=postgresql

After this you need to rebuild the docker image

docker-compose build

Than change your config/database.yml file

default: &default
  adapter: postgresql
  encoding: unicode
  host: db
  username: postgres
  password:
  pool: 5

development:
  <<: *default
  database: noteapp_development

test:
  <<: *default
  database: noteapp_test

Than you need to run

docker-compose up --build

in Dockerfile

   FROM ruby:2.5.1
   RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs

   RUN mkdir /noteapp
   WORKDIR /noteapp

   ADD Gemfile /noteapp/Gemfile

   RUN bundle install

   ADD . /noteapp
   CMD ["rails","server","-b","0.0.0.0"]

Remove this ADD Gemfile.lock /noteapp/Gemfile.lock add this CMD ["rails","server","-b","0.0.0.0"]

in docker-compose

version: '2' 
services: 
   db:
     image: postgres
   web: 
     build: .
     volumes:
       - .:/noteapp
     ports:
       - "3000:3000"
     depends_on:
       - db

add tty and stdin_open options in your docker-compose.yml

version: '2' 
services:
   db:
      image: postgres
   web: 
      build: .
      stdin_open: true
      tty: true
      command: bundle exec rails s -p 3000 -b '0.0.0.0'
      volumes:
        - .:/noteapp
      ports:
        - "3000:3000"
      depends_on:
        - db

then run

docker-compose build

and finally you can run

docker-compose up 

or in dettached mode

docker-compose up -d
Related