【问题标题】:Can't start rails application via docker无法通过 docker 启动 rails 应用程序
【发布时间】:2018-09-19 14:24:33
【问题描述】:

我尝试使用 Ruby on Rails 应用程序构建 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"]

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

运行命令后

sudo docker-compose up --build

我得到了结果,noteapp_web_1 exited with code 0 执行此操作后,我尝试启动 docker 容器,一切正常,但我的应用程序在 localhost:3000 上没有响应

当我尝试查看日志时(sudo docker logs id),我得到了结果 console_result

但是当我尝试连接到我的容器时,我不能这样做,并且出现错误:

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

我应该怎么做才能运行我的应用程序?

更新,我使用了@Upendra Chahar 的解决方案 并遇到了这个问题 rails_error

rake_error

之后我解决了这个问题,现在我遇到了 postgresql db 的问题: postgresql_db

【问题讨论】:

    标签: ruby-on-rails docker docker-compose dockerfile


    【解决方案1】:

    首先,您需要运行此命令以在容器中创建 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 up --build
    

    【讨论】:

    • 非常感谢。我尝试了您的解决方案,但在最后一步,当我运行 docker-compose up --build 时,我遇到了一些问题,不知道该怎么办。 @Upendra Chahar,我在顶部附上了屏幕截图。
    • 在 gemfile 中检查你的 rails 版本
    • 在 gemfile 中检查您的 rails 版本并更改为 gem 'rails', '5.2.0' 并将 docker-compose.yml 版本更改为 version: '3'
    • 我做了,但是docker-compose对我说,没有第3版,不知道为什么,我试了一下ti research,因为有第3版
    • K... 但是你需要在 gemfile 中检查你的 rails 版本
    【解决方案2】:

    在 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"]
    

    删除这个 ADD Gemfile.lock /noteapp/Gemfile.lock 添加这个 CMD ["rails","server","-b","0.0.0.0"]

    在 docker-compose 中

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

    【讨论】:

    • 很抱歉,您的解决方案对我不起作用。我更新了对我的问题的描述,我试图做什么以及结果是什么。 @Gioggi
    【解决方案3】:

    在 docker-compose.yml 中添加 tty 和 stdin_open 选项

    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
    

    【讨论】:

      猜你喜欢
      • 2012-09-12
      • 2010-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 2021-01-22
      • 2011-05-18
      • 1970-01-01
      相关资源
      最近更新 更多