I have a PHP application that also includes some static files (images, scripts, etc.). I want to deploy this application to a Docker swarm – how do I ensure that the nginx tasks have access to the application files (which I assume would be built inside the php-fpm container)?
I thought about using a volume that both containers share. From what I understand, Docker would copy the files from the php-fpm (app) container into the volume, and nginx would then be able to access them as well. However, there are multiple issues:
- The nginx and php-fpm tasks might not be on the same node, so the volume would be empty on an nginx-only node.
- Updating the php-fpm (app) image might not update the volume files – if I understand correctly, Docker won’t update files in the volume from files in the container if the volume is not empty/new.
This is surely a fairly common setup – how is it usually handled?
Some alternatives that I’ve thought of:
- Create a volume that is not tied to a specific node, i.e. one that uses a non-local volume driver for example.
- Seems overly complicated, and may be difficult to update with application changes.
- Find a way to ensure that nginx tasks are always deployed along with php-fpm (app) tasks.
- As far as I know, this is not possible, and would also have the volume update issue.
- Put nginx and php-fpm (and the app files) in the same image.
- This goes against Docker best practices, wouldn’t scale well, etc.
- Create an nginx image with static files, and a php-fpm image with PHP files.
- The application is in a single repository, and I was hoping to have a single Dockerfile to build the application image.
- This would require building two images from the same repository, and making sure that each one gets only the files that it needs (ideally).
- Use Apache instead of nginx, which has a single image with support for PHP (as a module).
- I would prefer to continue using nginx.
- Configure nginx/php-fpm to serve the static files through PHP.
- Not ideal for performance reasons, but if there’s a CDN in front of the files anyway, then very few requests would be routed to PHP.
I don’t really like any of these options, but I’m beginning to think that there is no clean and standard way of doing this. I hope that I’m wrong.