我的rails项目中有一个Dockerfile,结尾为:
CMD ["bundle", "exec", "unicorn", "-D", "-c /usr/src/app/config/unicorn.rb"]
当我运行一个容器时,我得到:
/usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/lib/unicorn/configurator.rb:75:in `read': No such file or directory @ rb_sysopen - /usr/src/app/config/unicorn.rb (Errno::ENOENT)
from /usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/lib/unicorn/configurator.rb:75:in `reload'
from /usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/lib/unicorn/configurator.rb:68:in `initialize'
from /usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:100:in `new'
from /usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/lib/unicorn/http_server.rb:100:in `initialize'
from /usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/bin/unicorn:126:in `new'
from /usr/local/lib/ruby/gems/2.1.0/gems/unicorn-4.8.3/bin/unicorn:126:in `<top (required)>'
from /usr/local/bin/unicorn:23:in `load'
from /usr/local/bin/unicorn:23:in `<main>'
master failed to start, check stderr log for details
我不明白,因为当我将CMD更改为CMD ["cat", "config/unicorn.rb"]
时,docker run <image>
会正确输出文件。
经过一番尝试和错误后,我发现:
CMD bundle exec unicorn -D -c /usr/src/app/config/unicorn.rb
正常运作。
在咨询Docker docs后,我发现在使用格式CMD ["executable","param1","param2"]
时,需要为可执行文件提供完整路径。我在最初的尝试中没有这样做。
我继续将CMD更改为:
CMD ["/usr/local/bin/bundle", "exec", "unicorn", "-D", "-c /usr/src/app/config/unicorn.rb"]
工作!
令我困惑的是,在初始尝试中bundle exec unicorn
仍在运行,但却找不到配置文件。
有人可以冒充这种行为吗?为什么我的初始尝试运行,但找不到文件,其他两个版本正常运行?
对于任何想要重现这一点的人。这是Dockerfile:
FROM benjaminbauer/ruby:latest
ONBUILD EXPOSE 8080
CMD ["/usr/local/bin/bundle", "exec", "unicorn", "-D", "-c /usr/src/app/config/unicorn.rb"]
benjaminbauer/ruby
只是official ruby image cached bundle install的一个分支。如果您想构建它,请使用cache_bundle_install
branch。