【问题标题】:Rails api Docker container fails on startup (failed to load command: rails) when started with other services与其他服务一起启动时,Rails api Docker 容器在启动时失败(无法加载命令:rails)
【发布时间】:2020-02-27 09:48:29
【问题描述】:

我有几个通过 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

【问题讨论】:

  • 我很久以前就构建了它,以使 Rails 在容器内启动,大部分问题来自 GEM envs,看看:github.com/noxsnono/Docker/blob/master/ruby232_sql_rails/…
  • 您的docker-compose.yml 指定了一个不同的command(带有重复的bundle exec),并且正在用volumes: 覆盖您的应用程序的Gemfile 等内容。从docker-compose.yml 文件中删除这些行有帮助吗?
  • entrypoint.sh 中有什么?

标签: docker docker-compose rails-api docker-image


【解决方案1】:

尝试用这个在 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'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-18
    • 2023-04-11
    • 2016-05-06
    • 2011-06-19
    相关资源
    最近更新 更多