Ruby Gemfile.lock 在不同系统下表现不一致怎么办?

willx · 2021年09月23日 · 最后由 filetype 回复于 2021年10月10日 · 793 次阅读

我尝试使用 Docker 统一开发环境与生产环境,但是发现每次 bundle install 之后 Gemfile 生成的 lock 文件会不一样,并且会影响到部分代码的运行。

比如我先在 mac 下进行 bundle install,再在 Docker(debian)下进行 bundle 后,Gemfile.lock 就变了,并且有一些 rails 命令在 Docker 中失效了。

如果我把 lock 删掉,先在 Docker 中 install,然后在 Mac 下 install,RubyMine 就没办法执行 rspec 的测试了:

Unable to find a spec satisfying nokogiri (~> 1.8) in the set. Perhaps the lockfile is corrupted? Found nokogiri (1.12.4-x86_64-linux), nokogiri (1.12.4-x86_64-linux), nokogiri (1.12.4-x86_64-linux), nokogiri (1.12.4-x86_64-linux), nokogiri (1.12.4-x86_64-linux) that did not match the current platform.

😭 😭 😭 😭 😭 😭 😭

像 Nodejs 的 lock 文件就不会出现这种问题

Dockerfile 中先拷贝 Gemfile Gemfile.lock,执行 bundle install 之后,再 copy 所有代码

我一直都这么干,没发现什么问题啊。。。

nokogiri 安装的是预编译版,在 linux bundle 锁的是 linux 版,在 mac bundle 锁的是 mac 版。

我没同时用这两个环境不知道怎么解决。如果用 docker 统一环境,我想应该配置 IDE 在 docker 里面执行 rspec。

我遇到了类似问题,运行了 bundle update 就解决了。

都加入到 lock 文件即可。如下

nokogiri (1.12.4-x86_64-darwin)
  racc (~> 1.4)
nokogiri (1.12.4-x86_64-linux)
  racc (~> 1.4)

操作流程:在 mac 下执行一遍,再用 docker compose 跑一下 bundle insall,把项目文件挂载到容器中,就会生成类似上面的 lock 文件。

rails 命令失效的问题,应该是你的容器镜像中环境变量没有配置好。可以使用 ./bin/rails 代替 rails

在开发环境添加需要支持的 platform:

PLATFORM=x86_64-linux
bundle lock --add-platform ${PLATFORM}
bundle install

如果用 alpine 镜像,则 PLATFORM=x86_64-linux-musl

上述命令会更改 Gemfile.lock 以支持指定的 platform。

rails app:update:bin

需要 登录 后方可回复, 如果你还没有账号请 注册新账号