M2芯片Mac构建GitLab CI镜像遇Spring启动超时问题求助
问题:ARM架构构建的GitLab CI镜像中Spring启动超时导致CI失败
我有一个基于Rails框架的应用,整套CI流程部署在GitLab上。首次构建GitLab CI镜像(存储于GitLab Registry)时遇到Spring相关问题:使用搭载M2芯片的MacBook构建的镜像,在GitLab Runner中Spring无法正常启动,启动超时20秒,进而导致「Migrations are pending.」错误,CI任务失败。本地环境无Spring异常及迁移问题,即使强制在rspec前执行迁移也会因相同原因失败。通过添加--platform linux/amd64参数构建CI镜像可解决该问题,但不清楚ARM架构的GitLab Runner上Spring出现此问题的原因。
好坏任务输出差异
正常任务输出:
[TEST PROF INFO] Spring detected
异常任务输出:
Starting Spring server with `/usr/local/bundle/gems/spring-4.1.1/bin/spring server --background` timed out after 20 seconds
镜像构建Dockerfile
FROM ruby:3.2.2 EXPOSE 3000 LABEL "maintainer"="benebrice" LABEL "app_type"="backend" LABEL "service_type"="API" USER root ARG RAILS_ENV=development ENV BUNDLER_VERSION 2.4.19 ENV APP /rails RUN apt-get update && apt-get install -y lsb-release # Yarn source 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-key adv --refresh-keys --keyserver keyserver.ubuntu.com # PSQL 14 RUN wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - RUN echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list RUN apt-get update -qq && apt-get install -y net-tools nodejs postgresql-client-14 cmake nano zsh unzip yarn python3-pip jq psmisc WORKDIR $APP # Fix slow IRB issue # issue: https://github.com/ruby/irb/issues/43 RUN echo 'IRB.conf[:USE_MULTILINE] = false' > ~/.irbrc RUN echo 'set linenumbers' > ~/.nanorc # Main app COPY Gemfile /rails/Gemfile COPY Gemfile.lock /rails/Gemfile.lock RUN gem install bundler -v $BUNDLER_VERSION # Main app RUN bundle install RUN gem clean RUN rm -r /rails/* # Add a script to be executed every time the container starts. COPY entrypoint.sh /usr/bin/ RUN chmod +x /usr/bin/entrypoint.sh # Start the main process. CMD ["bundle", "exec", "puma", "-C", "config/puma.rb", "config.ru"]
问题原因分析
- 架构转译性能损耗:M2芯片属于ARM64架构,默认构建的镜像也是ARM64格式。如果GitLab Runner运行在x86_64(amd64)架构服务器上,会通过QEMU进行指令转译来运行ARM64镜像。这种转译会带来明显的性能下降,Spring启动需要加载大量Rails环境资源,转译导致的性能损耗直接触发了20秒的超时阈值。
- 架构适配差异:部分Ruby gem(包括Spring)在ARM架构上的性能表现或兼容性不如x86_64架构,叠加QEMU转译的额外开销后,启动时间被进一步拉长,最终导致超时失败。
解决方案
构建镜像时添加--platform linux/amd64参数,强制生成x86_64架构的镜像。这样GitLab Runner无需进行指令转译即可直接运行镜像,Spring启动性能恢复正常,超时问题得以解决。
内容的提问来源于stack exchange,提问作者brcebn
相关产品推荐
相关产品推荐

