简体   繁体   English

Rails 图像未从 docker-compose.yml 运行

[英]Rails image not running from docker-compose.yml

I have a rails application that runs on Docker.我有一个在 Docker 上运行的 Rails 应用程序。 My source code have the following files:我的源代码有以下文件:

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 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

It runs normally using docker-compose up since I'm running this with the source code level.它使用docker-compose up正常运行,因为我在源代码级别运行它。

Now when I build this app and push it to Dockerhub现在,当我构建这个应用程序并将其推送到 Dockerhub 时

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

I'm expecting that I can run the rails app from an independent docker-compose.yml separately from the source code.我期待我可以从独立于源代码的独立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

The only containers running are redis and db .唯一运行的容器是redisdb The web is looking for this web正在寻找这个

Could not locate Gemfile or .bundle/ directory

In the second docker-compose.yml file, the one that should work somewhere else without the source code, you're still having the volume mounting the local folder in the container:在第二docker-compose.yml文件中,该文件应该在没有源代码的情况下在其他地方工作,您仍然将卷安装在容器中的本地文件夹中:

  volumes: 
    - .:/myapp

Remove that from the sidekiq and web containers and it should work.sidekiqweb容器中删除它,它应该可以工作。

You've also kept the build: .您还保留了build: . for the sidekiq container which is useful only for the development box.对于仅对开发箱有用的sidekiq容器。 Replace it with the image attribute, pointing to your image.将其替换为image属性,指向您的图像。

To summarise your docker-comspose.yaml file:总结您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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM