如何在 Docker 主机上更新 Gemfile.lock?

How do I update Gemfile.lock on my Docker host?

在 Docker 容器内使用 Rails 时 several posts, (including one on docker.com) 使用以下模式:

  1. Dockerfile中执行ADD GemfileADD Gemfile.lock,然后RUN bundle install
  2. 使用 docker-compose run web rails new 创建一个新的 Rails 应用。

既然我们RUN bundle install构建了镜像,那么docker-compose build web更新后好像比较合适Gemfile.

这有效,因为 gemset 将在图像内更新,但是:

Docker 主机 上的 Gemfile.lock 将不会更新 以反映对 Gemfile 的更改。这是一个问题,因为:

  1. Gemfile.lockshould be in your repository,以及:

  2. 应该与你现在的Gemfile一致


所以:

如何更新主机上的 Gemfile.lock,以便将其签入版本控制?

run 中执行 bundle 是否 更新主机上的 Gemfile.lock

docker-compose run web bundle

但是:您仍然必须 build 图像。

TL;DR - 在容器上进行更改,运行 捆绑在容器上并重新启动以备不时之需.在本地,这些更改将反映在您的应用程序中,并准备好 test/push 到 git,您的生产服务器将使用它来重建。

长;阅读:

  1. docker exec -it name_of_app_1 bash

  2. vim Gemfile 并添加类似 gem 'sorcery', '0.9.0' 的内容 我觉得这可以确保您获得所需的版本

  3. bundle 在当前容器的 GemfileGemfile.lock

  4. 中仅获取此版本

这是半正常的 "Rails" 类型的东西,只是你在 运行ning 的容器上做。现在您不必担心 git 并进行这些更改。您的 git 存储库,因为这些更改都发生在您的本地副本上。比如,打开一个终端选项卡并进入您的应用程序,然后 less Gemfile 您应该会看到更改。

  1. 现在您可以重新启动 运行ning 容器。您的 Dockerfile 将被重建(在我的例子中,通过 docker-compose up 在本地重建,测试应该通过。随意进行浏览器测试。

提交对 git 的更改,并使用您的部署过程在暂存时检查它。

备注:

检查 Dockerfile 是否像 OP 的链接所说的那样。我假设您的 Dockerfile

中有某种 bundlebundle install

为了清楚起见,运行 的命令是:

docker-compose run web bundle
docker-compose up --build

其中 web 是您的 Dockerized Rails 应用程序的名称。

运行 bundle update 在容器里面,然后重建。

$ docker-compose run app bundle update
$ docker-compose build