重建后文件更改未反映在 Docker 映像中

Files changes not reflected in Docker image after rebuild

我正在尝试为我的 PHP 网络应用程序 (php-fcm) 设置两个 Docker 图像,由 NGINX 反向代理。理想情况下,我希望将 Web 应用程序的所有文件复制到基于 php-fcm 的映像中并作为一个卷公开。这样,两个容器(网络和应用程序)都可以使用 NGINX 服务静态文件和 php-fcm 解释 php 文件来访问文件。

docker-compose.yml

version: '2'
services:
  web:
    image: nginx:latest
    depends_on:
      - app
    volumes:
      - ./site.conf:/etc/nginx/conf.d/default.conf
    volumes_from:
      - app
    links:
      - app
  app:
    build: .
    volumes:
      - /app

Docker文件:

FROM php:fpm
COPY . /app
WORKDIR /app

以上设置按预期工作。但是,当我对文件进行任何更改然后执行

compose up --build

生成的图像中未拾取新文件。尽管有以下消息表明图像确实正在重建:

Building app
Step 1 : FROM php:fpm
 ---> cb4faea80358
Step 2 : COPY . /app
 ---> Using cache
 ---> 660ab4731bec
Step 3 : WORKDIR /app
 ---> Using cache
 ---> d5b2e4fa97f2
Successfully built d5b2e4fa97f2

只有删除所有旧图像才能解决问题。

知道是什么原因造成的吗?

$ docker --version
Docker version 1.11.2, build b9f10c9
$ docker-compose --version
docker-compose version 1.7.1, build 0a9ab35

'volumes_from' 选项将卷从一个容器装载到另一个容器。那里的重要词是容器,而不是图像。所以重建镜像的时候,之前的容器还是运行。如果您停止并重新启动该容器,或者只是停止它,其他容器仍在使用那些旧的挂载点。如果您停止,删除旧的应用程序容器,然后启动一个新的应用程序容器,旧的卷挂载仍将保留到现在已删除的容器中。

根据您的情况,解决此问题的更好方法是切换到命名卷并设置一个实用程序容器来更新此卷。

version: '2'
volumes:
  app-data:
    driver: local

services:
  web:
    image: nginx:latest
    depends_on:
      - app
    volumes:
      - ./site.conf:/etc/nginx/conf.d/default.conf
      - app-data:/app
  app:
    build: .
    volumes:
      - app-data:/app

用于更新您的应用程序数据量的实用程序容器可能类似于:

docker run --rm -it \
  -v `pwd`/new-app:/source -v app-data:/target \
   busybox /bin/sh -c "tar -cC /source . | tar -xC /target"

正如 BMitch 指出的那样,图像更新不会自动过滤到容器中。您的更新工作流程需要重新审视。我刚刚完成了构建一个包含 NGINX 和 PHP-FPM 的容器的过程。我发现,对我来说,最好的方法是将 nginx 和 php 包含在一个容器中,它们都由 supervisord 管理。 然后我在图像中有脚本,允许您从 git 存储库更新代码。这使得整个过程非常简单。

#Create new container from image
docker run -d --name=your_website -p 80:80 -p 443:443 camw/centos-nginx-php
#git clone to get website code from git
docker exec -ti your_website get https://www.github.com/user/your_repo.git
#restart container so that nginx config changes take effect
docker restart your_website

#Then to update, after committing changes to git, you'll call
docker exec -ti your_website update
#restart container if there are nginx config changes
docker restart your_website

我的容器可以在 https://hub.docker.com/r/camw/centos-nginx-php/ 找到 dockerfile 和相关的构建文件位于 https://github.com/CamW/centos-nginx-php

如果您想尝试一下,只需 fork https://github.com/CamW/centos-nginx-php-demo,按照自述文件中的指示更改 conf/nginx.conf 文件并包含您的代码。

这样做,你根本不需要处理体积,一切都在我喜欢的容器中。