'Why do we need to map the project files in both PHP-FPM and web server container?

I am pretty new with all of this docker stuff and I have this docker-compose.yml file:

fpm:
  build: 
    context: "./php-fpm"
    dockerfile: "my-fpm-dockerfile"
  restart: "always"
  ports:
    - "9002:9000"
  volumes:
    - ./src:/var/www/src
  depends_on:
    - "db"
  
nginx:
  build:
    context: "./nginx"
    dockerfile: "my-nginx-dockerfile"
  restart: "always"
  ports:
    - "8084:80"
  volumes:
    - ./docker/logs/nginx/:/var/log/nginx:cached
    - ./src:/var/www/src
  depends_on:
    - "fpm"

I am curious why do I need to add my project files in the fpm container as well as in the nginx?

Why isn't it enough to add it just only to my webserver? A web server is a place that holds the files and handles the request...

I believe that this information would be useful to other docker newbies as well.

Thanks in advance.



Solution 1:[1]

In your NGinx container you only need the statics and in your PHP-FPM container you only need the PHP files. If you are capable of splitting the files, you don't need any file in both sites.

Why isn't it enough to add it just only to my webserver? A web server is a place that holds the files and handles the request...

NGinx handles requests from users. If a request is to a static file (configured in NGinx site), it sends the contents back to the user. If the request is to a PHP file (and NGinx is correctly configured to use FPM on that place), it sends the request to the FPM server (via socket or TCP request), which knows how to execute PHP files (NGinx doesn't know that). You can use PHP-FPM or whatever other interpreter you prefer, but this one works great when configured correctly.

Solution 2:[2]

If you just want an explanation why both need the access to the same files under /var/www/src, I cannot provide a reliable answer since I’m not familiar neither with nginx not fpm.
But I can provide an explanation what’s the purpose of doing it so.


First, to learn about docker, I highly recommend the official documentation, since it provides a great explanation: docs.docker.com

For learning the syntax of a docker-compose file, see docs.docker.com: Compose file reference


Your specific question

Let me break down what you have her:
You got two different images, fpm and nginx.

fpm:
 ...
  
nginx:
 ...

In principle, these containers (or services as they are called ) run completely independent from each other. This basically means, that they don't know that the other one exists.
Note: depends_on just expresses a dependency between services

Conclusion: Your webserver knows nothing about your second container.

As said: While I don't know the purpose of fpm, I assume that a common folder is the connection between these two containers. By using a common folder ./src on your host, they both have access to this ./src folder, thus can write to and read from it. The syntax ./src:/var/www/src means, that this ./src folder is mapped (internally in your container) to :/var/www/src. If a container writes to /var/www/src it will actually write to ./src on your host. This works vice versa.

Conclusion: They share a common directory where both containers can access the very same files.

Hope my explanation helps you understanting your docker-compose better.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 agentsmith