rails: Default Dockerfile is broken for arm64 builds on Ruby 3.3.6 due to a Bootsnap bug

I’m on an Intel mac and the Docker build works if I leave the architecture as default. When targeting arm64 with --platform linux/arm64 however, it hangs with no output from bootsnap.

If I use docker buildx build . then it works fine. Downgrading to Ruby 3.2.6 also works fine.

Same issue on Bootsnap GitHub: https://github.com/Shopify/bootsnap/issues/495

The problematic line in the dockerfile is:

RUN bundle install && \
    rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git && \
    bundle exec bootsnap precompile --gemfile

Breaking it apart into this shows that it’s bootsnap, but gives no ouput:

RUN bundle install
RUN rm -rf ~/.bundle/ "${BUNDLE_PATH}"/ruby/*/cache "${BUNDLE_PATH}"/ruby/*/bundler/gems/*/.git
RUN bundle exec bootsnap precompile --gemfile --verbose

Steps to reproduce

  1. Install Ruby 3.3.6
  2. install rails 8.0.1 gem with gem install rails
  3. rails new docker_test
  4. cd docker_test
  5. docker buildx build --platform linux/arm64 .

Expected behaviour:

The docker build completes with no errors.

Actual behaviour:

Docker build hangs with the output from bundle install as the last lines on the console.

System configuration

Rails version: 8.0.1

Ruby version: 3.3.6

System: Intel MacOS

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 4
  • Comments: 15 (11 by maintainers)

Commits related to this issue

Most upvoted comments

Yes, that was my issue:

diff --git a/Dockerfile b/Dockerfile
index b4cfba9..af7eaa9 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -30,7 +30,7 @@ FROM base AS build

 # Install packages needed to build gems
 RUN apt-get update -qq && \
-    apt-get install --no-install-recommends -y build-essential git pkg-config && \
+    apt-get install --no-install-recommends -y build-essential git libyaml-dev pkg-config && \
     rm -rf /var/lib/apt/lists /var/cache/apt/archives

 # Install application gems

Still, the Dockerfile new out-of-box was broken…

Still, the Dockerfile new out-of-box was broken…

That’s because the base image changed without warning: https://github.com/rails/rails/pull/54237

Can’t believe we’re being blamed for this…

And yes that a totally different issue.

Fair enough. To summarize for anyone ending up here later:

The reasoning is that, since the issue is not reproducible on an M1 MacBook Pro, emulation is likely a contributing factor, potentially the primary one. Until this is better understood, there’s nothing that can be done here.

This kind of issue is beyond the expertise of most web developers. A practical workaround for now might be to rent a VPS with ARM CPUs for the image-building process.

By the way, mattgibson mentioned on the Bootsnap repository that he was able to sidestep the issue by downgrading Ruby to version 3.2.6.

There might, but in any case the issue can’t really be fixed on the Rails side, and probably not even in bootsnap, because it mostly just calls into Ruby APIs to store the bytecode.

The key to fix this is to find a proper reproduction, so we can figure out what the bug is exactly, assuming it’s not in QEMU itself (QEMU easily deadlock in various cases, so if it’s a QEMU limitation, there’s likely not much we can do).

The obvious workaround being to not use bootsnap, but it’s too useful to most people to justify removing it entirely from the default Dockerfile.

On a M1 Macbook Pro I can’t reproduce this - these are the variants I tried:

  • docker build .
  • docker buildx build --platform linux/amd64 .
  • docker buildx build --platform linux/arm64 .

After each attempt I cleared the build cache.

I wonder whether this is something in the ruby base image? Have you tried building from scratch using the source for the base image?

You can try this branch: https://github.com/iiewad/blog/tree/debug when i run docker buildx build --platform linux/amd64 . on Apple M1 Pro It is hang up too.

➜  blog git:(main) ✗ docker buildx build --platform linux/amd64 .
[+] Building 283.4s (16/23)                                                                                                                                                                                docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                                                                                                       0.0s
 => => transferring dockerfile: 2.47kB                                                                                                                                                                                     0.0s
 => resolve image config for docker-image://docker.io/docker/dockerfile:1                                                                                                                                                  1.8s
 => [auth] docker/dockerfile:pull token for registry-1.docker.io                                                                                                                                                           0.0s
 => CACHED docker-image://docker.io/docker/dockerfile:1@sha256:93bfd3b68c109427185cd78b4779fc82b484b0b7618e36d0f104d4d801e66d25                                                                                            0.0s
 => [internal] load build definition from Dockerfile                                                                                                                                                                       0.0s
 => [internal] load metadata for docker.io/library/ruby:3.3.5-slim                                                                                                                                                         1.6s
 => [auth] library/ruby:pull token for registry-1.docker.io                                                                                                                                                                0.0s
 => [internal] load .dockerignore                                                                                                                                                                                          0.0s
 => => transferring context: 902B                                                                                                                                                                                          0.0s
 => [base 1/3] FROM docker.io/library/ruby:3.3.5-slim@sha256:25a9df53c6f23406f6bc87426ad5bd74b6d99423a8c2ca630f2443dee2447f53                                                                                              0.0s
 => [internal] load build context                                                                                                                                                                                          0.1s
 => => transferring context: 15.90kB                                                                                                                                                                                       0.1s
 => CACHED [base 2/3] WORKDIR /rails                                                                                                                                                                                       0.0s
 => CACHED [base 3/3] RUN apt-get update -qq &&     apt-get install --no-install-recommends -y curl libjemalloc2 libvips sqlite3 &&     rm -rf /var/lib/apt/lists /var/cache/apt/archives                                  0.0s
 => CACHED [build 1/8] RUN apt-get update -qq &&     apt-get install --no-install-recommends -y build-essential git pkg-config &&     rm -rf /var/lib/apt/lists /var/cache/apt/archives                                    0.0s
 => CACHED [build 2/8] COPY Gemfile Gemfile.lock ./                                                                                                                                                                        0.0s
 => CACHED [build 3/8] RUN bundle install                                                                                                                                                                                  0.0s
 => CACHED [build 4/8] RUN rm -rf ~/.bundle/ "/usr/local/bundle"/ruby/*/cache "/usr/local/bundle"/ruby/*/bundler/gems/*/.git                                                                                               0.0s
 => [build 5/8] RUN bundle exec bootsnap precompile --gemfile --verbose                                                                                                                                                  279.8s
 => => # /usr/local/bundle/ruby/3.3.0/gems/activerecord-8.0.0/lib/active_record/locale/en.yml
 => => # /usr/local/bundle/ruby/3.3.0/gems/activemodel-8.0.0/lib/active_model/locale/en.yml
 => => # /usr/local/bundle/ruby/3.3.0/gems/actionview-8.0.0/lib/action_view/locale/en.yml
 => => # /usr/local/bundle/ruby/3.3.0/gems/nokogiri-1.16.7-x86_64-linux/dependencies.yml
 => => # /usr/local/bundle/ruby/3.3.0/gems/activesupport-8.0.0/lib/active_support/locale/en.yml
 => => # /usr/local/bundle/ruby/3.3.0/gems/actiontext-8.0.0/package.json