Rails映像未从docker-compose.yml

时间:2019-12-12 14:28:16

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

我有一个在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

1 个答案:

答案 0 :(得分:1)

在第二个docker-compose.yml文件中,该文件应在没有源代码的情况下可以在其他地方使用,您仍然可以通过该卷将本地文件夹装入容器:

  volumes: 
    - .:/myapp

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

您还为build: .容器保留了sidekiq容器,该容器仅对开发箱有用。将其替换为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