【问题标题】:Rails image not running from docker-compose.ymlRails 映像未从 docker-compose.yml 运行
【发布时间】:2019-12-12 14:28:16
【问题描述】:

我有一个在 Docker 上运行的 rails 应用程序。我的源代码有以下文件:

Dockerfile

FROM ruby:2.6.0
RUN apt-get update -qq && 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
CMD bash -c "rm -f tmp/pids/server.pid && rails s -p 3000 -b '0.0.0.0'"

docker-compose.yml

version: '3'
services:
 db:
  image: postgres
  volumes:
   - ./tmp/db:/var/lib/postgresql/data
  ports:
    - "5432:5432"
 redis:
  image: redis
  command: redis-server
  ports: 
    - "6379:6379"
 sidekiq:
  build: .
  command: bundle exec sidekiq
  depends_on:
    - redis
  volumes: 
    - .:/myapp
 web:
  build: .
  command: bash -c "rm -f tmp/pids/server.pid && rails s -p 3000 -b '0.0.0.0'"
  volumes:
   - .:/myapp
  ports:
   - "3000:3000"
  depends_on:
   - db
   - redis
   - sidekiq

它使用docker-compose up 正常运行,因为我在源代码级别运行它。

现在当我构建这个应用程序并将其推送到 Dockerhub 时

docker build -t myusername/rails-app .
docker push myusername/rails-app

我希望我可以从独立的docker-compose.yml 与源代码分开运行 rails 应用程序。

version: '3'
services:
 db:
  image: postgres
  volumes:
   - ./tmp/db:/var/lib/postgresql/data
  ports:
    - "5432:5432"
 redis:
  image: redis
  command: redis-server
  ports: 
    - "6379:6379"
 sidekiq:
  build: .
  command: bundle exec sidekiq
  depends_on:
    - redis
  volumes: 
    - .:/myapp
 web:
  image: myusername/rails-app:latest # <= Running the app now from the image
  command: bash -c "rm -f tmp/pids/server.pid && rails s -p 3000 -b '0.0.0.0'"
  volumes:
   - .:/myapp
  ports:
   - "3000:3000"
  depends_on:
   - db
   - redis
   - sidekiq

唯一运行的容器是redisdbweb 正在寻找这个

Could not locate Gemfile or .bundle/ directory

【问题讨论】:

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


    【解决方案1】:

    在第二个 docker-compose.yml 文件中,该文件应该在没有源代码的情况下在其他地方工作,您仍然有卷在容器中安装本地文件夹:

      volumes: 
        - .:/myapp
    

    sidekiqweb 容器中删除它,它应该可以工作。

    您还为 sidekiq 容器保留了 build: .,该容器仅对开发框有用。将其替换为 image 属性,指向您的图像。

    总结您的docker-comspose.yaml 文件:

    version: '3'
    services:
     db:
      image: postgres
      volumes:
       - ./tmp/db:/var/lib/postgresql/data
      ports:
        - "5432:5432"
     redis:
      image: redis
      command: redis-server
      ports: 
        - "6379:6379"
     sidekiq:
      image: myusername/rails-app:latest
      command: bundle exec sidekiq
      depends_on:
        - redis
     web:
      image: myusername/rails-app:latest # <= Running the app now from the image
      command: bash -c "rm -f tmp/pids/server.pid && rails s -p 3000 -b '0.0.0.0'"
      ports:
       - "3000:3000"
      depends_on:
       - db
       - redis
       - sidekiq
    

    【讨论】:

    • sidekiq 怎么样,我也应该更改'build:.'它返回错误请将 Sidekiq 指向 Rails 应用程序或 Ruby 文件。
    • 哦,是的,我错过了,build 没用,因为我猜它不是开发环境。
    • 当我从sidekiq中删除构建时显示错误“服务sidekiq既没有指定图像也没有指定构建上下文。至少必须提供一个。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多