What you decide to do here depends on your use case.
For local development, mounting volumes is often required so you can have code on your local machine that the containers can see.
A note about PHP-FPM with Nginx: The most common Nginx configuration to use with php-fpm actually requires that Nginx "sees" the *.php file on the server before it decides to send it to php-fpm instead. If it doesn't find the file, it returns a 404 error. This is why if you have Nginx and PHP-FPM in separate containers, they both often need volume mounts.
I tend to install php-fpm and Nginx in one container for this reason. See Vessel for an example of that.
For production, code is often built into a Docker image (via COPY), and that container can then be deployed wherever based on that new image with that code.
I do that method, and tag each new image with the commit sha (and "retag" the latest image as tag latest - so the same image is tagged twice). This way I have a history of built images at each commit sha.
@snguimjeu the nginx config doesn't make sense since it does not actually have access to any files therefore how could root /var/www/html/public; even work?
@martinszeltins I'm currently playing around with this particular setup and there are a couple of things playing together:
This part will redirect to index.php
In this week I stumbled into this problem myself. My solution was this docker documentation in docs.docker.com/storage/ which says:
If you mount an empty volume into a directory in the container in which files or directories exist, these files or directories are propagated (copied) into the volume. Similarly, if you start a container and specify a volume which does not already exist, an empty volume is created for you. This is a good way to pre-populate data that another container needs.
So my solution was just to mount an empty volume under
After this, the static files were perfectly served to nginx from the php-fpm container which in this case has all the code that needed to be shared with nginx.