Bundle install failes on docker
<p>I use Docker in my Rails app.
Now I try do build it on new machine and suffer some problems.</p>

<p>When I run <code>docker-compose -f dcom-dev.yml run --user $(id -u) web bundle install</code>
it starts to work, but failes with</p>

<pre><code>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.
</code></pre>

<p>If I try <code>docker-compose -f dcom-dev.yml run --user $(id -u) web gem install debug_inspector -v '0.0.2'</code> it successfully executes, telling that gem's intalled.</p>

<p>Also during installing bundle produces terrible output like:</p>

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

<p>or</p>

<pre><code>sudo: unknown uid 1000: who are you?
</code></pre>

<p><code>whoami</code> runs without any problems, so I don't know, why it asks me so strange questions :)</p>

<p><code>dcom-dev.yml</code>:</p>

<pre><code>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: &amp;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:
    &lt;&lt;: *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
</code></pre>

<p>Part of <code>Dockerfile</code> that's about ruby:</p>

<pre><code>FROM buildpack-deps:trusty

# Ruby

# skip installing gem documentation
RUN mkdir -p /usr/local/etc \
  &amp;&amp; { \
    echo 'install: --no-document'; \
    echo 'update: --no-document'; \
  } &gt;&gt; /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 \
  &amp;&amp; buildDeps=' \
    bison \
    libgdbm-dev \
    ruby \
  ' \
  &amp;&amp; apt-get update \
  &amp;&amp; apt-get install -y --no-install-recommends $buildDeps \
  &amp;&amp; rm -rf /var/lib/apt/lists/* \
  &amp;&amp; curl -fSL -o ruby.tar.gz "http://cache.ruby-lang.org/pub/ruby/$RUBY_MAJOR/ruby-$RUBY_VERSION.tar.gz" \
  &amp;&amp; echo "$RUBY_DOWNLOAD_SHA256 *ruby.tar.gz" | sha256sum -c - \
  &amp;&amp; mkdir -p /usr/src/ruby \
  &amp;&amp; tar -xzf ruby.tar.gz -C /usr/src/ruby --strip-components=1 \
  &amp;&amp; rm ruby.tar.gz \
  &amp;&amp; cd /usr/src/ruby \
  &amp;&amp; { echo '#define ENABLE_PATH_CHECK 0'; echo; cat file.c; } &gt; file.c.new &amp;&amp; mv file.c.new file.c \
  &amp;&amp; autoconf \
  &amp;&amp; ./configure --disable-install-doc \
  &amp;&amp; make -j"$(nproc)" \
  &amp;&amp; make install \
  &amp;&amp; apt-get purge -y --auto-remove $buildDeps \
  &amp;&amp; gem update --system $RUBYGEMS_VERSION \
  &amp;&amp; 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" \
  &amp;&amp; chmod 777 "$GEM_HOME" "$BUNDLE_BIN"
</code></pre>

<p>===============Upd 1================</p>

<p>Here's output for <code>bundle instal --verbose</code></p>

<pre><code>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'
</code></pre>

<p>I can see it tries to use ruby 2.3.0 while my Gemfile specified with 2.3.1. Usually it says smth about it, but now keeps silent.</p>

<p>What can be the problem with all this? And what should I do? Thanks!</p>

<p>==========Upd 2==================
I've realised it could be better if I run command without <code>--user $(id -u)</code>, so I run <code>docker-compose -f dcom-dev.yml run web bundle install</code>, and it gives me such output:</p>

<pre><code>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&gt;&amp;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.
</code></pre>

<p>Obviously,<code>gem install debug_inspector -v '0.0.2'</code> succeeds.
Also I don't know, if I should use <code>--user</code> or not. If not, I run bundler as sudo and that's bad, if I use, user has no permissions to write to bundler folder.</p>