如何解决Apple M1/M1 Pro芯片环境下Docker容器中Rails创建RSpec测试时的「Function not implemented - Failed to initialize inotify (Errno::ENOSYS)」错误
Errno::ENOSYS (inotify initialization failed) in Dockerized Rails on Apple Silicon Hey there! I know this error can feel frustrating when you're just getting started with Ruby and Rails, so let's walk through some practical fixes tailored to your setup (Apple M1/M1 Pro + Docker + RSpec).
Why this happens
The error comes from the listen gem trying to use Linux's inotify system to watch for file changes, but your Docker environment on Apple Silicon doesn't support this properly—either because the gem is misdetecting the environment, or the filesystem mount isn't set up to work with inotify.
Step 1: Upgrade the listen gem
Older versions of listen (like your 3.1.5) don't handle Apple Silicon's Docker environment well. Upgrading to a newer version fixes the environment detection:
- Open your
Gemfileand update the listen line to:gem 'listen', '>= 3.7.0' - Run
bundle update listento install the new version - Stop Spring (the Rails preloader) with
spring stopto apply changes
Step 2: Force polling mode if upgrading doesn't work
If the gem still tries to use inotify, you can force it to use polling (a cross-platform file-watching method):
- Option 1: Add a Spring config file
Createconfig/spring.rb(if it doesn't exist) with this code:Spring.watchers = [Spring::Watcher::Polling.new] - Option 2: Run RSpec with an environment variable
Execute your tests with this command instead:LISTEN_GEM_ADAPTER=poll bundle exec rspec
Step 3: Fix Docker volume mounts
Poorly configured volume mounts can break file watching. Update your docker-compose.yml (or Docker run command) to use a compatible mount option:
volumes: - .:/app:delegated # This improves filesystem compatibility between macOS and Docker
The delegated option tells Docker to prioritize macOS's filesystem handling, which helps with file change detection.
Step 4: Disable Spring temporarily (for testing)
Spring (the Rails preloader) can sometimes cause issues with watchers. Test if the error goes away by running:
DISABLE_SPRING=1 bundle exec rspec
If this works, you know the problem is tied to Spring's watcher, and Step 2 should fix it permanently.
Try these steps one by one—most folks fix this with Step 1 or 2. Let me know if you hit any snags!
内容的提问来源于stack exchange,提问作者olenderpj

