docker build error Gem::Ext::BuildError: ERROR: Failed to build gem native extension for mimemagic-0.3.9

docker build error Gem::Ext::BuildError: ERROR: Failed to build gem native extension for mimemagic-0.3.9

我必须在我的本地机器上构建一个 docker 图像来验证它是否正在构建一个图像,但是我在本地机器上构建 docker 图像时遇到了这个问题.

 Installing mime magic 0.3.9 with native extensions
    Gem::Ext::BuildError: ERROR: Failed to build gem native extension.

    current directory: /usr/local/bundle/gems/mimemagic-0.3.9/ext/mimemagic
/usr/local/bin/ruby -I/usr/local/lib/ruby/2.7.0/rubygems -rrubygems
/usr/local/lib/ruby/gems/2.7.0/gems/rake-13.0.1/exe/rake
RUBYARCHDIR\=/usr/local/bundle/extensions/x86_64-linux/2.7.0/mimemagic-0.3.9
RUBYLIBDIR\=/usr/local/bundle/extensions/x86_64-linux/2.7.0/mimemagic-0.3.9
rake aborted!
Could not find MIME type database in the following locations:
["/usr/local/share/mime/packages/freedesktop.org.xml",
"/opt/homebrew/share/mime/packages/freedesktop.org.xml",
"/usr/share/mime/packages/freedesktop.org.xml"]


/usr/local/bundle/gems/mimemagic-0.3.9/ext/mimemagic/Rakefile:25:in `block in
<top (required)>'
Tasks: TOP => default
(See full trace by running task with --trace)

rake failed, exit code 1

Gem files will remain installed in /usr/local/bundle/gems/mimemagic-0.3.9 for
inspection.
Results logged to
/usr/local/bundle/extensions/x86_64-linux/2.7.0/mimemagic-0.3.9/gem_make.out

An error occurred while installing mimemagic (0.3.9), and Bundler cannot
continue.
Make sure that `gem install mimemagic -v '0.3.9' --source
'https://rubygems.org/'` succeeds before bundling.

如果您使用的是 macOS,请尝试以下操作

 brew install shared-mime-info
 bundle update mimemagic

并尝试捆绑您的 gem 文件

  1. 修改Dockerfile以安装shared-mime-info包。例如。在基于 Debian 的图像上:

    RUN apt-get update && apt-get install -y shared-mime-info

  2. 如果还是不行,那么您可能需要更新 mimemagic gem。在您的主机上,更新 Rails 应用的 Gemfile/Gemfile.lock 中的 mimemagic。您可能需要先安装shared-mime-info:如果主机是macOS,您可能需要运行 brew install shared-mime-info;如果主持人是 Ubuntu,您可能需要 运行 apt-get install shared-mime-info。然后运行

    bundle update mimemagic

  3. 如果您的 Dockerfile 从存储库下载 Rails 应用程序,请先将您的更改推送到该存储库。或者,为了测试,修改 Dockerfile 以改为从主机复制 Rails 应用程序。