捆绑安装在 docker 上失败

Bundle install failes on docker

我在 Rails 应用程序中使用 Docker。 现在我尝试在新机器上构建它并遇到一些问题。

当我运行docker-compose -f dcom-dev.yml run --user $(id -u) web bundle install 它开始工作,但失败

An error occurred while installing debug_inspector (0.0.2), and Bundler cannot continue.
Make sure that `gem install debug_inspector -v '0.0.2'` succeeds before bundling.

如果我尝试 docker-compose -f dcom-dev.yml run --user $(id -u) web gem install debug_inspector -v '0.0.2' 它会成功执行,表明 gem 已安装。

在安装 bundle 期间也会产生可怕的输出,如:

Errno::EACCES: Permission denied @ rb_sysopen - /bundler_cache/extensions/x86_64-linux/2.3.0-static/pg-0.18.4/gem_make.out

sudo: unknown uid 1000: who are you?

whoami 运行s 没有任何问题,所以我不知道,为什么它问我这么奇怪的问题:)

dcom-dev.yml:

version: '2'
services:
  db:
    image: postgres
    volumes_from:
      - db_data
  nginx:
    build:
      context: ./service/nginx
    ports:
      - "1935:1935"
      - "80:80"
    depends_on:
      - web
    volumes_from:
      - records
  processor:
    build:
      context: ./service/processor
    ports:
      - "3322:22"
    volumes_from:
      - records
  web: &app_base
    build: .
    command: bundle exec rails s -p 3000 -b '0.0.0.0'
    volumes_from:
      - bundler_cache
    volumes:
      - .:/app
    ports:
      - "3000:3000"
    depends_on:
      - db
      - processor
    env_file: .env
  worker:
    <<: *app_base
    ports: []
    command: bundle exec rake jobs:work
  db_data:
    image: busybox
    volumes:
      - /var/lib/postgresql/data
  records:
    image: busybox
    volumes:
      - /records
  bundler_cache:
    image: busybox
    volumes:
      - /bundler_cache

Dockerfile 的一部分,大约 ruby:

FROM buildpack-deps:trusty

# Ruby

# skip installing gem documentation
RUN mkdir -p /usr/local/etc \
  && { \
    echo 'install: --no-document'; \
    echo 'update: --no-document'; \
  } >> /usr/local/etc/gemrc

ENV RUBY_MAJOR 2.3
ENV RUBY_VERSION 2.3.1
ENV RUBY_DOWNLOAD_SHA256 b87c738cb2032bf4920fef8e3864dc5cf8eae9d89d8d523ce0236945c5797dcd
ENV RUBYGEMS_VERSION 2.6.6

# some of ruby's build scripts are written in ruby
# we purge this later to make sure our final image uses what we just built
RUN set -ex \
  && buildDeps=' \
    bison \
    libgdbm-dev \
    ruby \
  ' \
  && apt-get update \
  && apt-get install -y --no-install-recommends $buildDeps \
  && rm -rf /var/lib/apt/lists/* \
  && curl -fSL -o ruby.tar.gz "http://cache.ruby-lang.org/pub/ruby/$RUBY_MAJOR/ruby-$RUBY_VERSION.tar.gz" \
  && echo "$RUBY_DOWNLOAD_SHA256 *ruby.tar.gz" | sha256sum -c - \
  && mkdir -p /usr/src/ruby \
  && tar -xzf ruby.tar.gz -C /usr/src/ruby --strip-components=1 \
  && rm ruby.tar.gz \
  && cd /usr/src/ruby \
  && { echo '#define ENABLE_PATH_CHECK 0'; echo; cat file.c; } > file.c.new && mv file.c.new file.c \
  && autoconf \
  && ./configure --disable-install-doc \
  && make -j"$(nproc)" \
  && make install \
  && apt-get purge -y --auto-remove $buildDeps \
  && gem update --system $RUBYGEMS_VERSION \
  && rm -r /usr/src/ruby

ENV BUNDLER_VERSION 1.12.5

RUN gem install bundler --version "$BUNDLER_VERSION"

# install things globally, for great justice
# and don't create ".bundle" in all our apps
ENV GEM_HOME /usr/local/bundle
ENV BUNDLE_PATH="$GEM_HOME" \
  BUNDLE_BIN="$GEM_HOME/bin" \
  BUNDLE_SILENCE_ROOT_WARNING=1 \
  BUNDLE_APP_CONFIG="$GEM_HOME"
ENV PATH $BUNDLE_BIN:$PATH
RUN mkdir -p "$GEM_HOME" "$BUNDLE_BIN" \
  && chmod 777 "$GEM_HOME" "$BUNDLE_BIN"

===============更新 1================

这是 bundle instal --verbose

的输出
Errno::EACCES: Permission denied @ rb_sysopen - /bundler_cache/extensions/x86_64-linux/2.3.0-static/debug_inspector-0.0.2/gem_make.out
/usr/local/lib/ruby/2.3.0/open-uri.rb:37:in `initialize'
/usr/local/lib/ruby/2.3.0/open-uri.rb:37:in `open'
/usr/local/lib/ruby/2.3.0/open-uri.rb:37:in `open'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:215:in `write_gem_make_out'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:135:in `build_error'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:174:in `rescue in build_extension'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:159:in `build_extension'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:201:in `block in build_extensions'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:198:in `each'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:198:in `build_extensions'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/installer.rb:747:in `build_extensions'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/installer.rb:299:in `install'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/source/rubygems.rb:143:in `block in install'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/rubygems_integration.rb:169:in `preserve_paths'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/source/rubygems.rb:135:in `install'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/installer/gem_installer.rb:57:in `install'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/installer/gem_installer.rb:15:in `install_from_spec'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/installer/parallel_installer.rb:91:in `block in worker_pool'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/worker.rb:65:in `apply_func'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/worker.rb:60:in `block in process_queue'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/worker.rb:57:in `loop'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/worker.rb:57:in `process_queue'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/worker.rb:29:in `block (2 levels) in initialize'

我可以看到它尝试使用 ruby 2.3.0,而我的 Gemfile 指定为 2.3.1。平时都是嘴上说着,现在沉默了。

这一切可能有什么问题?我该怎么办?谢谢!

==========更新2================== 我已经意识到如果我 运行 命令没有 --user $(id -u) 会更好,所以我 运行 docker-compose -f dcom-dev.yml run web bundle install,它给了我这样的输出:

Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /bundler_cache/gems/nokogiri-1.6.7.2/ext/nokogiri
/usr/local/bin/ruby -r ./siteconf20160904-1-1ej0stt.rb extconf.rb
Cannot allocate memory - /usr/local/bin/ruby -r ./siteconf20160904-1-1ej0stt.rb extconf.rb 2>&1

Gem files will remain installed in /bundler_cache/gems/nokogiri-1.6.7.2 for inspection.
Results logged to /bundler_cache/extensions/x86_64-linux/2.3.0-static/nokogiri-1.6.7.2/gem_make.out
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:76:in ``'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:76:in `run'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/ext_conf_builder.rb:48:in `block in build'
/usr/local/lib/ruby/2.3.0/tempfile.rb:295:in `open'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/ext_conf_builder.rb:31:in `build'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:164:in `block (2 levels) in build_extension'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:163:in `chdir'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:163:in `block in build_extension'
/usr/local/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:162:in `build_extension'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:201:in `block in build_extensions'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:198:in `each'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/ext/builder.rb:198:in `build_extensions'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/installer.rb:747:in `build_extensions'
/usr/local/lib/ruby/site_ruby/2.3.0/rubygems/installer.rb:299:in `install'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/source/rubygems.rb:143:in `block in install'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/rubygems_integration.rb:169:in `preserve_paths'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/source/rubygems.rb:135:in `install'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/installer/gem_installer.rb:57:in `install'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/installer/gem_installer.rb:15:in `install_from_spec'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/installer/parallel_installer.rb:91:in `block in worker_pool'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/worker.rb:65:in `apply_func'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/worker.rb:60:in `block in process_queue'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/worker.rb:57:in `loop'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/worker.rb:57:in `process_queue'
/usr/local/lib/ruby/gems/2.3.0/gems/bundler-1.12.5/lib/bundler/worker.rb:29:in `block (2 levels) in initialize'
An error occurred while installing debug_inspector (0.0.2), and Bundler cannot
continue.
Make sure that `gem install debug_inspector -v '0.0.2'` succeeds before
bundling.

显然,gem install debug_inspector -v '0.0.2' 成功了。 我也不知道是否应该使用 --user 。如果没有,我 运行 bundler as sudo 这很糟糕,如果我使用,用户没有权限写入 bundler 文件夹。

您的用户 ID 在您的新机器上不同,您似乎已将此用户 ID 硬编码到您的图像中,至少是以文件权限的形式。标志 --user $(id -u) 告诉 docker-compose 以 运行 你的容器作为你主机上的用户,1000 在你的新机器上,并且 uid 无权访问你在你的内部创建的文件图片。

为了获得更完整的答案,我们需要了解您的 dcom-dev.yml 的内容以及您用于构建映像的任何 Dockerfile。

知道了! 由于我在 DigitalOcean 上使用了一个 droplet,我设法找到一个错误 Cannot allocate memory 意味着我需要创建一个交换。这真的很有帮助。

问题是here