简体   繁体   中英

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. 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. This way both containers (web and app) can access the files with NGINX serving the static files and php-fcm interpreting the php files.

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

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. 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. 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. I've found, for me, that the best way was to include nginx and php in a single container, both managed by supervisord. I then have scripts in the image that allow you to update your code from a 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

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.

Doing it this way, you don't need to deal with volumes at all, everything is in your container which I like.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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