【问题标题】:Files changes not reflected in Docker image after rebuild重建后文件更改未反映在 Docker 映像中
【发布时间】:2016-10-13 08:40:33
【问题描述】:

我正在尝试为我的 PHP Web 应用程序 (php-fcm) 设置两个由 NGINX 反向代理的 Docker 映像。理想情况下,我希望将 Web 应用程序的所有文件复制到基于 php-fcm 的图像中并作为一个卷公开。这样,两个容器(Web 和应用程序)都可以使用 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

Dockerfile:

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

【问题讨论】:

    标签: php nginx docker docker-compose docker-volume


    【解决方案1】:

    “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"
    

    【讨论】:

    • 谢谢,这确实是根本原因。我最终决定将数据分成两部分(静态和应用程序)并将它们复制到各自的图像中。这是有道理的,因为在后期我们可能希望在不同的服务器上同时运行。
    【解决方案2】:

    正如 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 文件并包含您的代码。

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

    【讨论】:

    • 谢谢分享。会试一试的!
    猜你喜欢
    • 2020-12-20
    • 1970-01-01
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多