Ruby on rails Can';t通过docker启动rails应用程序

Ruby on rails Can';t通过docker启动rails应用程序,ruby-on-rails,docker,docker-compose,dockerfile,Ruby On Rails,Docker,Docker Compose,Dockerfile,我试图用RubyonRails应用程序构建Docker容器,但做不到 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

我试图用RubyonRails应用程序构建Docker容器,但做不到

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"]
   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"]
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
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
运行命令后

sudo docker-compose up --build
我得到了结果, 在这个操作之后,我尝试启动docker容器,一切正常,但我的应用程序在localhost:3000上没有响应

当我试图查看日志(SudoDocker日志id)时,我得到了结果

但当我尝试连接到我的容器时,我无法执行此操作,并出现错误:

sudo docker attach ee43805fd6cf
You cannot attach to a stopped container, start it first
我应该如何运行我的应用程序

更新,我使用了@Upendra Chahar的解决方案 有这个问题吗

之后我解决了这个问题,现在我遇到了postgresql db的问题: 在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"]
   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"]
删除此添加Gemfile.lock/noteapp/Gemfile.lock 添加此CMD[“rails”、“服务器”、“-b”、“0.0.0.0”]

在docker compose中

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

首先,您需要运行此命令在容器中创建rails项目

docker-compose run web rails new . --force --database=postgresql
在此之后,您需要重建docker映像

docker-compose build
然后更改config/database.yml文件

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

development:
  <<: *default
  database: noteapp_development

test:
  <<: *default
  database: noteapp_test

在docker-compose.yml中添加tty和stdin_打开选项

   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
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
然后跑

docker-compose build
最后你可以跑了

docker-compose up 
或者在分离模式下

docker-compose up -d

非常感谢。我尝试了您的解决方案,但在最后一步,当我运行docker compose-up-build时,rails遇到了一些问题,我不知道该怎么办@Upendra Chahar,我在顶部附上了截图。在gemfile中检查你的rails版本在gemfile中检查你的rails版本并更改为gem'rails',5.2.0',还将docker-compose.yml版本更改为版本:“3”我做了,但docker compose对我说,没有版本3,我不知道为什么,我尝试了ti研究,因为第三个版本是现在的。。。但是你需要在Gemfile中检查你的rails版本,很抱歉,你的解决方案对我不起作用。我更新了对我的问题的描述,我试图做什么以及结果是什么@焦吉