如何解决Docker化Ruby on Rails应用中的‘stack smashing detected’错误
问题:MacOS更新至13.4.1(c)后Docker化Ruby on Rails应用崩溃
使用2023款16英寸M2 MacBook Pro,此前正常运行的Docker化Ruby on Rails应用,在将Mac操作系统更新至13.4.1 (c) (22F770820d)后出现问题。重新构建部署应用容器后,开发环境加载页面时容器崩溃,日志输出如下:
2023-07-24 16:03:33 (0.1ms) SELECT pg_try_advisory_lock(3213395767516697805); 2023-07-24 16:03:33 ActiveRecord::SchemaMigration Load (0.3ms) SELECT "schema_migrations".* FROM "schema_migrations" 2023-07-24 16:03:33 ActiveRecord::InternalMetadata Load (0.2ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2 [["key", "environment"], ["LIMIT", 1]] 2023-07-24 16:03:33 (0.1ms) BEGIN 2023-07-24 16:03:33 (0.1ms) COMMIT 2023-07-24 16:03:33 (0.1ms) SELECT pg_advisory_unlock(3213395767516697805) 2023-07-24 16:03:33 ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations" 2023-07-24 16:03:36 *** stack smashing detected ***: /usr/local/bundle/bin/rake assets:precompile terminated 2023-07-24 16:03:36 ./entrypoint.sh: line 5: 17 Aborted bundle exec rake assets:precompile 2023-07-24 16:03:36 Puma starting in single mode... 2023-07-24 16:03:36 * Version 3.12.1 (ruby 2.5.1-p57), codename: Llamas in Pajamas 2023-07-24 16:03:36 * Min threads: 5, max threads: 5 2023-07-24 16:03:36 * Environment: development 2023-07-24 16:03:38 * Listening on tcp://0.0.0.0:3000 2023-07-24 16:03:38 Use Ctrl-C to stop 2023-07-24 16:03:44 Started GET "/" for 10.0.0.2 at 2023-07-24 16:03:44 -0400 2023-07-24 16:03:44 Cannot render console from 10.0.0.2! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 2023-07-24 16:03:44 ActiveRecord::SchemaMigration Load (0.6ms) SELECT "schema_migrations".* FROM "schema_migrations" 2023-07-24 16:03:44 Processing by PagesController#splash as HTML 2023-07-24 16:03:44 Rendering pages/splash.haml within layouts/application 2023-07-24 16:03:44 *** stack smashing detected ***: puma 3.12.1 (tcp://0.0.0.0:3000) [app] terminated 2023-07-24 16:03:44 ./entrypoint.sh: line 11: 29 Aborted bundle exec puma -C config/puma.rb
已尝试大幅提升Docker资源配置但无效,关键Docker文件如下:
docker-compose.local.yml
version: '3.7' services: web: image: game-fuse_web build: context: . dockerfile: ./Dockerfile.web.local restart: always depends_on: - app volumes: - '.:/var/www/game-fuse-ast' ports: - 80:80 db: image: postgres:9.6-bullseye env_file: - .env volumes: - "db:/var/lib/postgresql/data" environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=password - POSTGRES_DB=game-fuse_development ports: - "5432:5432" app: stdin_open: true tty: true image: game-fuse_app build: context: . dockerfile: ./Dockerfile.local command: ./entrypoint.sh volumes: - .:/app ports: - 3000:3000 working_dir: /app depends_on: - db volumes: db:
Dockerfile.local
FROM ruby:2.5.1-slim ENV TZ=America/New_York # Allow recently archived stretch to be accessed RUN echo "deb http://archive.debian.org/debian stretch main" > /etc/apt/sources.list && \ sed -i 's/deb.debian.org/debian.osuosl.org/g' /etc/apt/sources.list && \ sed -i 's/security.debian.org/debian.osuosl.org/g' /etc/apt/sources.list RUN apt-get update -qq && apt-get install -y build-essential cron libpq-dev nodejs git RUN gem install nokogiri -v 1.12.5 RUN gem install net-protocol -v 0.1.2 RUN gem install net-smtp -v 0.3.0 RUN gem install net-imap -v 0.2.2 #RUN gem install -N rails # Set an environment variable where the Rails app is installed to inside of Docker image ENV RAILS_ROOT /var/www/game-fuse RUN mkdir -p $RAILS_ROOT # Set working directory WORKDIR $RAILS_ROOT # Setting env up ENV RAILS_ENV='development' ENV RACK_ENV='development' # Remove reliance on git:// protocol RUN git config --global url."https://".insteadOf git:// # Adding gems COPY Gemfile Gemfile COPY Gemfile.lock Gemfile.lock COPY config ./config # Adding project files RUN gem install bundler -v 2.3.26 RUN bundle install --jobs 20 --retry 5 RUN cd $RAILS_ROOT && bundle exec whenever --update-crontab
entrypoint.sh
#!/bin/bash bundle exec rake db:migrate bundle exec rake assets:precompile #bundle exec whenever --update-crontab #cron bundle exec puma -C config/puma.rb
解决方案
- 调整Docker Desktop的Rosetta设置:M系列Mac上,Docker Desktop默认启用Rosetta转译x86镜像。尝试关闭该功能(设置→Features in development→取消勾选"Use Rosetta for x86/amd64 emulation on Apple Silicon"),然后重新构建镜像并启动容器。部分旧版本Ruby/Rails依赖在Rosetta转译下会出现栈溢出问题。
- 升级Ruby版本:Ruby 2.5.1是较旧版本,对Apple Silicon的Rosetta转译兼容性较差。尝试升级到Ruby 2.7+或更高版本,更新Dockerfile中的基础镜像(如
FROM ruby:2.7.8-slim),重新安装依赖并构建容器。 - 禁用资产预编译的并行处理:在
assets:precompile命令中添加--no-parallel参数,修改entrypoint.sh中的对应命令为bundle exec rake assets:precompile --no-parallel,避免并行编译导致的栈溢出。 - 检查并更新依赖gem:查看Gemfile.lock中依赖的gem版本,特别是nokogiri、puma这类底层扩展gem,更新到兼容Ruby 2.5.1和Apple Silicon的版本,重新执行
bundle install并构建镜像。 - 清理Docker缓存:执行
docker system prune -a清理旧镜像和缓存,确保重新构建时使用最新的依赖和系统库,避免缓存中的旧文件导致兼容性问题。
内容的提问来源于stack exchange,提问作者MingMan
相关产品推荐
相关产品推荐

