Docker-compose 和 rails 控制台问题。 'Could not find rake-13.0.6 in any of the sources'

Docker-compose and rails console issue. 'Could not find rake-13.0.6 in any of the sources'

在 docker 下尝试 运行 rails 控制台时遇到一些问题。所有其他 rails 命令都按预期工作,但控制台没有。

➜  octopus git:(master) ✗ docker-compose run web bundle exec rails c
Creating octopus_web_run ... done
Could not find rake-13.0.6 in any of the sources
Run `bundle install` to install missing gems.
ERROR: 1

rake 已安装。

➜  octopus git:(master) ✗ docker-compose run web bundle install
Creating octopus_web_run ... done
Using rake 13.0.6
Using concurrent-ruby 1.1.9
Using i18n 1.8.11
....
Bundle complete! 42 Gemfile dependencies, 144 gems now installed.
Bundled gems are installed into `./.bundle`

更多

➜  octopus git:(master) ✗ docker-compose run web bundle config list
Creating octopus_web_run ... done
Settings are listed in order of priority. The top value will be used.
app_config
Set via BUNDLE_APP_CONFIG: "/myapp/.bundle"

silence_root_warning
Set via BUNDLE_SILENCE_ROOT_WARNING: true

path
Set via BUNDLE_PATH: "/myapp/.bundle"

bin
Set via BUNDLE_BIN: "/bin"
➜  octopus git:(master) ✗ docker-compose run web bundle exec rails s -p 3000 -b '0.0.0.0'
Creating octopus_web_run ... done
=> Booting Puma
=> Rails 6.1.4.4 application starting in development
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.5.2 (ruby 2.7.5-p203) ("Zawgyi")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 1
* Listening on http://0.0.0.0:3000
Use Ctrl-C to stop
^C- Gracefully stopping, waiting for requests to finish
=== puma shutdown: 2022-01-17 19:28:14 +0000 ===
- Goodbye!
Exiting

而且 docker-compose up 照常工作。

我使用过许多类似的 docker 设置,没有任何问题。我运行没主意了。

环境设置

Docker文件

FROM ruby:2.7

RUN curl -fsSL https://deb.nodesource.com/setup_17.x | bash -

# Install yarn, for webpack
RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add -
RUN echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list

RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs yarn

RUN mkdir /myapp
WORKDIR /myapp

COPY Gemfile /myapp/Gemfile
COPY Gemfile.lock /myapp/Gemfile.lock

ENV GEM_HOME /myapp/.bundle
ENV BUNDLE_PATH=$GEM_HOME \
  BUNDLE_APP_CONFIG=$BUNDLE_PATH \
  BUNDLE_BIN=$BUNDLE_PATH/bin
ENV PATH /app/bin:$BUNDLE_BIN:$PATH
ENV NODE_OPTIONS=--openssl-legacy-provider

Docker-compose.yml

version: '3.9'
services:
  db:
    image: postgres:13
    volumes:
      - octopus-db-sync:/var/lib/postgresql/data:nocopy
    environment:
      POSTGRES_PASSWORD: password
  redis:
    image: redis
  web:
    build: .
    command: ./start_docker_development.sh
    volumes:
      - octopus-sync:/myapp:nocopy
    environment:
      BUNDLE_APP_CONFIG: /myapp/.bundle
    ports:
      - "3000:3000"
    depends_on:
      - db
      - redis
      - mailcatcher
  nginx:
    image: nginx
    volumes:
      - ./nginx-develop.conf:/etc/nginx/conf.d/default.conf:z
      - octopus-sync:/myapp:nocopy
    ports:
      - "80:80"
    expose:
      - "80"
    depends_on:
      - web
  mailcatcher:
    image: sj26/mailcatcher
    ports:
      - "1080:1080"
volumes:
  octopus-sync:
    external: true
  octopus-db-sync:
    external: true

start_docker_development.sh

#!/bin/bash

bundle check || bundle install
rm -f tmp/pids/server.pid
echo > log/development.log
echo > log/test.log
bundle exec rails s -p 3000 -b '0.0.0.0'

非常感谢任何帮助。

每次执行 docker-compose run 都会创建一个新容器,因此之前安装的 gem 不再可用。

为了解决您的问题,您可以 运行 bundle install 在 Dockerfile 中,将 gems 安装在 app 文件夹中 bundle install --path vendor/bundle 或者挂载 bundle 默认用于存储 gems 的目录, 通常 /usr/local/bundle

我发现这个问题是在 spring 4.0 (issue 669) 中引入的。束环境变量被忽略。

降级到 spring 3.1.1 修复问题。

Gemfile

gem 'spring', '~> 3.1.1'