Docker compose Volume\u在重建后不更新

Docker compose Volume\u在重建后不更新,docker,docker-compose,dockerfile,Docker,Docker Compose,Dockerfile,假设有两个容器:webserver(1)承载静态HTML文件,这些文件需要在数据卷容器(2)中构建表单模板 docker compose.yml文件如下所示: version: "2" services: webserver: build: ./web ports: - "80:80" volumes_from: - templates templates: build: ./templates image=builder:

假设有两个容器:webserver(1)承载静态HTML文件,这些文件需要在数据卷容器(2)中构建表单模板

docker compose.yml
文件如下所示:

version: "2"

services:
  webserver:
    build: ./web
    ports:
      - "80:80"
    volumes_from:
      - templates
  templates:
    build: ./templates
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]
模板的
Dockerfile
服务如下所示

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
时,一切都按预期工作:模板已生成,Web服务器托管它们,您可以在浏览器中查看它们

问题是,当我更新
/source
并重新启动/重建安装程序时,Web服务器主机上的文件仍然是旧文件,尽管日志显示容器已重建-至少是
复制/source/tmp/source
之后的最后三层。因此,
source
文件夹中的更改将被重新生成的文件拾取,但我无法获取浏览器中显示的更改

我做错了什么?

撰写,这可能就是您看到旧文件的原因

一般来说,将卷用于源代码(或者在本例中是静态html文件)不是一个好主意。卷用于存储要持久化的数据,就像数据库中的数据一样。源代码随映像的每个版本而变化,因此并不真正属于卷

您可以使用
builder
容器编译这些文件,并使用
webserver
服务托管这些文件,而不是为这些文件使用数据卷容器。您需要将
副本
添加到
webserver
Dockerfile
以包含文件

要完成此操作,请将您的
docker compose.yml
更改为:

version: "2"
services:
  webserver:
    image: myapp:latest
    ports: ["80:80"]
现在您只需要构建
myapp:latest
。您可以编写一个脚本:

  • 构建生成器容器
  • 运行生成器容器
  • 构建
    myapp
    容器
您也可以使用类似的工具,而不是编写脚本(免责声明:我是此工具的作者)。有一个和你想做的非常相似的例子

您的
dobi.yaml
可能如下所示:

version: "2"

services:
  webserver:
    build: ./web
    ports:
      - "80:80"
    volumes_from:
      - templates
  templates:
    build: ./templates
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
,它将为您完成所有步骤。仅当文件已更改时,才会运行每个步骤。

谢谢您的回复。请查看
dobi
。与此同时,我还偶然发现了“命名卷”——它们能起作用吗?但我也认为它们还不能与docker cloud一起使用:(你可以运行一个容器来更新命名卷的内容,这将比来自
volumes\u更好。你知道docker cloud现在是否支持命名卷吗?