在重build之后,Docker组合volumes_from不会更新

设想两个容器:webserver(1)托pipe静态HTML文件,需要在数据容器容器(2)内的表单模板中构build。

docker-compose.yml文件看起来像这样:

 version: "2" services: webserver: build: ./web ports: - "80:80" volumes_from: - templates templates: build: ./templates 

Dockerfile templates服务看起来像这样

 FROM ruby:2.3 # ... there is more but that is should not be important WORKDIR /tmp COPY ./Gemfile /tmp/Gemfile RUN bundle install COPY ./source /tmp/source RUN bundle exec middleman build --clean VOLUME /tmp/build 

当我运行docker-compose up一切都按预期工作:模板已经build好,web服务器托pipe它们,你可以在浏览器中查看它们。

问题是,当我更新./source并重新启动/重build安装程序时,虽然日志显示容器已被重build – 至less是COPY ./source /tmp/source后的最后三个层次,但Web服务器主机仍然是旧的COPY ./source /tmp/source 。 所以在source文件夹内的更改被拾取重build,但我无法得到在浏览器中显示的更改。

我究竟做错了什么?

在重新创build容器时,组合可以保留卷 ,这可能是您看到旧文件的原因。

一般来说,对源代码使用卷(或者在这种情况下是静态html文件)并不是一个好主意。 卷是要保留的数据,如数据库中的数据。 源代码随图像的每个版本而变化,因此不属于卷。

您可以使用builder容器来编译这些文件,并使用一个webserver来承载这些文件,而不是使用数据卷容器。 您需要添加一个COPYwebserver Dockerfile来包含这些文件。

要做到这一点,你会改变你docker-compose.yml到这个:

 version: "2" services: webserver: image: myapp:latest ports: ["80:80"] 

现在你只需要build立myapp:latest 。 你可以写一个脚本:

  • build立build设者容器
  • 运行构build器容器
  • build立myapp容器

你也可以使用像dobi这样的工具来代替编写脚本(免责声明:我是这个工具的作者)。 有一个例子,build立一个最小的docker图像 ,这是非常类似于你想要做的。

你的dobi.yaml可能看起来像这样:

 image=builder: image: myapp-dev context: ./templates job=templates: use: builder image=webserver: image: myapp tags: [latest] context: . depends: [templates] compose=serve: files: [docker-compose.yml] depends: [webserver] 

现在,如果你运行dobi serve它会为你做所有的步骤。 每个步骤只有在文件改变的情况下才会运行。