title: multi-stage docker build with bundler
body: <pre><code>ARG     RUBY_VERSION=3.1.0
  ARG     IMAGE_OS=slim-buster
  FROM    ruby:${RUBY_VERSION}-${IMAGE_OS} as prod-gem-base
  ENV     APP_ENVIRONMENT=production 
  ENV     RACK_ENV=production
  ARG     CI_ACCESS_TOKEN
  WORKDIR /contract-service
  RUN     apt-get update &amp;&amp; apt-get install -qqy build-essential ca-certificates tzdata shared-mime-info git curl openssh-client libpq-dev &amp;&amp; \
          rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* &amp;&amp; \
          gem install bundler:2.3.13 &amp;&amp; \
          bundle config --global frozen 1 &amp;&amp; \
          bundle config set without 'development test' &amp;&amp; \
          mkdir -p -m 600 $HOME/.ssh &amp;&amp; ssh-keyscan gitlab.xxx.de &gt;&gt; $HOME/.ssh/known_hosts &amp;&amp; \
          if [ -n $CI_ACCESS_TOKEN ]; then git config --global url.&quot;https://gitlab-ci-token:${CI_ACCESS_TOKEN}@gitlab.xxx.de /&quot;.insteadOf git@gitlab.xxx.de: ; fi
  COPY    Gemfile Gemfile.lock ./
  RUN     --mount=type=ssh bundle install --jobs 20 --retry 5 &amp;&amp; \
          bundle clean --force &amp;&amp; \
          rm -rf /usr/local/bundle/cache/*.gem &amp;&amp; \
          find /usr/local/bundle/gems/ -name &quot;*.c&quot; -delete &amp;&amp; \
          find /usr/local/bundle/gems/ -name &quot;*.o&quot; -delete
  
  ARG     RUBY_VERSION
  ARG     IMAGE_OS
  FROM    ruby:${RUBY_VERSION}-${IMAGE_OS} as prod-core-base
  ENV     APP_ENVIRONMENT=production
  ENV     RACK_ENV=production
  WORKDIR /contract-service
  RUN     mkdir -p /tmp/pids &amp;&amp; apt-get update &amp;&amp; apt-get install -qqy --no-install-recommends shared-mime-info openssh-client libpq-dev &amp;&amp; \
  COPY    --from=prod-gem-base /usr/local/bundle/ /usr/local/bundle/
  COPY    . .
  EXPOSE  3005
  CMD     [&quot;bundle&quot;, &quot;exec&quot;, &quot;puma&quot;, &quot;--pidfile&quot;, &quot;/tmp/pids/server.pid&quot;, &quot;-b&quot;, &quot;tcp://0.0.0.0&quot;, &quot;-p&quot;, &quot;3005&quot;]
  </code></pre>
  <p>I am not sure how many ruby people are here. I am creating an with multi-stage build to cache the gems. I am building a production image but when the container is run the bundler outputs error as follows.</p>
  <pre><code>bundler: failed to load command: puma (/usr/local/bundle/bin/puma)
  cntrct-contract.1.dfhae8xfhdip@stage-app01-ruh2    | /usr/local/bundle/gems/bundler-2.3.13/lib/bundler/definition.rb:486:in `materialize': Could not find rubocop-1.31.2, byebug-11.1.3, simplecov-0.21.2, simplecov-cobertura-2.1.0, webmock-3.13.0, cucumber-8.0.0, rspec-3.11.0, rspec-core-3.11.0, rspec_junit_formatter-0.5.1, rack-test-2.0.2, json-2.6.2, parallel-1.22.1, parser-3.1.2.0, rainbow-3.1.1, regexp_parser-2.5.0, rubocop-ast-1.19.1, ruby-progressbar-1.11.0, unicode-display_width-2.2.0, docile-1.4.0, simplecov-html-0.12.3, simplecov_json_formatter-0.1.4, crack-0.4.5, hashdiff-1.0.1, builder-3.2.4, cucumber-ci-environment-9.0.4, cucumber-core-11.0.0, cucumber-cucumber-expressions-15.2.0, cucumber-gherkin-23.0.1, cucumber-html-formatter-19.2.0, cucumber-messages-18.0.0, diff-lcs-1.5.0, multi_test-1.1.0, sys-uname-1.2.2, rspec-expectations-3.11.0, rspec-mocks-3.11.1, rspec-support-3.11.0, ast-2.4.2, cucumber-tag-expressions-4.1.0, ffi-1.15.5 in any of the sources (Bundler::GemNotFound)
  cntrct-contract.1.dfhae8xfhdip@stage-app01-ruh2    |    from /usr/local/bundle/gems/bundler-2.3.13/lib/bundler/definition.rb:191:in `specs'
  cntrct-contract.1.8tfk50ss339q@stage-app01-ruh2    |    from /usr/local/bundle/bin/bundle:25:in `&lt;main&gt;'
  cntrct-contract.1.dfhae8xfhdip@stage-app01-ruh2    |    from /usr/local/bundle/gems/bundler-2.3.13/lib/bundler/definition.rb:239:in `specs_for'
  cntrct-contract.1.dfhae8xfhdip@stage-app01-ruh2    |    from /usr/local/bundle/gems/bundler-2.3.13/lib/bundler/runtime.rb:18:in `setup'
  cntrct-contract.1.dfhae8xfhdip@stage-app01-ruh2    |    from /usr/local/bundle/gems/bundler-2.3.13/lib/bundler.rb:151:in `setup'
  cntrct-contract.1.dfhae8xfhdip@stage-app01-ruh2    |    from /usr/local/bundle/gems/bundler-2.3.13/lib/bundler/setup.rb:20:in `block in &lt;top (required)&gt;'
  </code></pre>
  <p>The issues is bundler is trying to load the development and test related gems which is not in the final image. Can someone tell me how to do it correctly with multi-stage docker build ?</p>