I recently started to use composer-packaged applications within Docker images. But I noticed that the Docker image did not change when I rebuilt after I updated one of the module dependencies. The reason is the caching mechanism of docker builds. A very obvious solution would be the
--no-cache option when building. But this is a bad solution as it would cause the complete build to make use of the cache.
Another solution I found was using ARGs that will disable the cache for a command. I personally felt this not very helpful as it would involve some awkward tweak of the build process (as I understand). So I discovered a very simple solution: the
ADD command can skip the caching when using a web URL as the content to download. The command would download the content from the address and compare it to the previous cache content. We would just need a URL that returns a different content each time it is called. And luckily there is one.
So the elegant solution is to use this combination of lines in your Dockerfile and the composer modules are updated accordingly each time:
# The RUN must use cached layers, this is the hack ADD "https://www.random.org/cgi-bin/randbyte?nbytes=10&format=h" skipcache RUN composer install \ && chown -R www-data:www-data /var/www/html