简体   繁体   English

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

[英]Files changes not reflected in Docker image after rebuild

I'm trying to set up two Docker images for my PHP web application (php-fcm) reversed proxied by NGINX. 我正在尝试为我的NGINX代理的PHP Web应用程序(php-fcm)设置两个Docker映像。 Ideally I would like all the files of the web application to be copied into the php-fcm based image and exposed as a volume. 理想情况下,我希望将Web应用程序的所有文件复制到基于php-fcm的映像中并作为卷公开。 This way both containers (web and app) can access the files with NGINX serving the static files and php-fcm interpreting the php files. 这样,两个容器(Web和应用程序)都可以使用NGINX提供的静态文件和php-fcm解释php文件来访问文件。

docker-compose.yml 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: Dockerfile:

FROM php:fpm
COPY . /app
WORKDIR /app

The above setup works as expected. 以上设置按预期方式工作。 However, when I make any change to the files and then do 但是,当我对文件进行任何更改然后执行

compose up --build

the new files are not picked up in the resulting images. 新文件未在生成的图像中拾取。 This is despite the following message indicating that the image is indeed being rebuilt: 尽管有以下消息表明该映像确实正在重建,但这仍然是:

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

Only removing all the old images does the trick. 仅删除所有旧图像即可解决问题。

Any idea what could cause this? 知道是什么原因造成的吗?

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

The 'volumes_from' option mounts volumes from one container to another. 'volumes_from'选项将卷从一个容器装载到另一个容器。 The important word there is container, not image. 重要的词是容器,而不是图像。 So when you rebuild an image, the previous container is still running. 因此,当您重建映像时,先前的容器仍在运行。 If you stop and restart that container, or even just stop it, the other containers are still using those old mount points. 如果停止并重新启动该容器,或者甚至只是停止它,则其他容器仍在使用那些旧的挂载点。 If you stop, remove the old app container, and start a new one, the old volume mounts will still persist to the now deleted container. 如果停止,请删除旧的应用程序容器,然后启动一个新的应用程序容器,旧的卷装载仍将保留到现在已删除的容器中。

The better way to solve this in your situation is to switch to named volumes and setup a utility container to update this volume. 解决这种情况的更好方法是切换到命名卷并设置实用程序容器以更新该卷。

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

A utility container to update your app-data volume could look something like: 用于更新您的应用程序数据量的实用程序容器可能类似于:

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

As BMitch points out, image updates don't automatically filter down into containers. BMitch指出,图片更新不会自动过滤到容器中。 your workflow for updates needs to be revisited. 您的更新工作流程需要重新审查。 I've just gone through the process of building a container which includes NGINX and PHP-FPM. 我刚刚完成了构建包含NGINX和PHP-FPM的容器的过程。 I've found, for me, that the best way was to include nginx and php in a single container, both managed by supervisord. 我发现,对我而言,最好的方法是将nginx和php包含在一个容器中,这两个容器均由supervisor管理。 I then have scripts in the image that allow you to update your code from a git repo. 然后,我在图像中有脚本,可让您从git repo更新代码。 This makes the whole process really easy. 这使整个过程非常容易。

#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

My container can be found at https://hub.docker.com/r/camw/centos-nginx-php/ The dockerfile and associated build files are available at https://github.com/CamW/centos-nginx-php 我的容器可以在https://hub.docker.com/r/camw/centos-nginx-php/上找到dockerfile和相关的构建文件可以在https://github.com/CamW/centos-nginx-php上找到

If you want to give it a try, just fork https://github.com/CamW/centos-nginx-php-demo , change the conf/nginx.conf file as indicated in the readme and include your code. 如果您想尝试一下,只需派生https://github.com/CamW/centos-nginx-php-demo ,按照自述文件中的指示更改conf / nginx.conf文件,并包含您的代码。

Doing it this way, you don't need to deal with volumes at all, everything is in your container which I like. 这样,您根本不需要处理卷,所有内容都在您喜欢的容器中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM