首页 > 解决方案 > 与其他服务一起启动时,Rails api Docker 容器在启动时失败(无法加载命令:rails)

问题描述

我有几个通过 docker-compose 文件一起运行的 docker 服务。

version: '3'
services:
  redis-server:
    image: redis
  scraper:
    image: davidgeismar/artifacts-scraper:without-db
    command: 'ruby ./scrape_sources.rb'
    environment:
      - REDIS_URL=redis://redis-server:6379/0
      - ARTIFACTS_ENV=docker_development
      - DATA_API_BASE=http://data_api:3000
    volumes:
      - .:/usr/src/artifacts_scraper
    depends_on:
      - data_api
      - redis-server
  data_api:
    image: davidgeismar/artifacts_data_api:latest
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes:
      - .:/artifacts_data_api
    environment:
      - RAILS_ENV=docker_development
      - SECRET_KEY_BASE=docker_development_secret
  sidekiq:
    build: .
    command: 'bundle exec sidekiq -r ./artifacts_scraper.rb 2>&1 | tee ./log/sidekiq.log'
    volumes:
      - ./:/usr/src/artifacts_scraper
    environment:
      - REDIS_URL=redis://redis-server:6379/0
      - ARTIFACTS_ENV=docker_development
      - DATA_API_BASE=http://data_api:3000
    depends_on:
      - redis-server
      - data_api

问题在于 data_api (一个 rails api)服务,当我使用它的 docker 文件单独运行它时,它工作正常:

FROM ruby:2.6.3
RUN apt-get update -qq && apt-get install -y nodejs postgresql-client
RUN mkdir /artifacts_data_api
WORKDIR /artifacts_data_api
COPY Gemfile /artifacts_data_api/Gemfile
COPY Gemfile.lock /artifacts_data_api/Gemfile.lock
RUN gem install bundler
RUN gem install rails
RUN bundle install
COPY . /artifacts_data_api

# Add a script to be executed every time the container starts.
COPY entrypoint.sh /usr/bin/
RUN chmod +x /usr/bin/entrypoint.sh
ENTRYPOINT ["/usr/bin/entrypoint.sh"]
EXPOSE 3000

# Start the main process.
CMD ["rails", "server", "-b", "0.0.0.0"]

但是,当我通过 docker-compose 文件与其他服务一起运行它时:docker-compose pull && docker-compose up --build

我得到:

data_api_1      | bundler: failed to load command: rails (/usr/local/bundle/ruby/2.6.0/bin/rails)
data_api_1      | Bundler::GemNotFound: Could not find concurrent-ruby-1.1.5 in any of the sources
data_api_1      |   /usr/local/bundle/gems/bundler-2.1.4/lib/bundler/spec_set.rb:86:in `block in materialize'

我不知道该服务在自行启动时如何运行良好,并且在通过 docker-compose 与其他服务一起运行时在启动时失败。

你可以在这里找到 github 项目:

https://github.com/davidgeismar/artifacts-scraper/tree/removing-db https://github.com/davidgeismar/artifacts_data_api

在 docker hub 上:

https://hub.docker.com/repository/docker/davidgeismar/artifacts-scraper

https://hub.docker.com/repository/docker/davidgeismar/artifacts_data_api

标签: dockerdocker-composerails-apidocker-image

解决方案


尝试用这个在 dockerfile 中启动你的图像

CMD bundle exec rails s -p 3000 -b '0.0.0.0'

并在 data_api 下的 docker-compose.yml 中删除此行

command: bundle exec rails s -p 3000 -b '0.0.0.0'

推荐阅读