Nginx + php-fpm: in which container should my php-code go?
Hey guys, I'm new to docker but I could get all things to run that I needed. Now I come across a bit of a problem with my actual php-webapp.
Scenario: A compose file with service nginx, and a service php-fpm. It's all working fine, I can scale the php-fpm containers and the webcode (cakephp) is working just fine. But currently I link in my webapp-code via host volume to a path /code into nginx service AND also into the php-fpm containers. If I remove the volume link from any service it will fail with either 404 not found (nginx) or file not (php-fpm).
So my problem in one line: when I build my images for production, I don't want to copy my code in both images. Which of the services should have my code in it, and how can I tell nginx or php-fpm where to look for the code? I also have an image with both nginx and php-fpm which also works fine, but I try to be able to scale the php containers, not the nginx (which perfectly works now, but I want to include the code in one of the services-images instead of running the code from a host volume). Sorry if this is a stupid question, I'm still learning
Posted Apr 17, 2018, 11:42 PM UTC
Not a stupid question, this is a common problem. This depends on where you’re running these containers in production...
Certain platforms such as AWS ECS provide a similar pattern to Docker compose’s ‘volumes_from’, allowing you to specify that Nginx’s ‘/var/www’ (or whatever your doc root is) should be mounted from PHP-FPM’s ‘/var/www’ - this is typically how this is setup locally.
In the case of Kubernetes, you can use initContainers to copy the contents of the PHP-FPM container’s web root directory into a shared volume with the Nginx container prior to a pod starting.
Either way, it’s logical to keep the application code in your PHP image/container and mount it into the Nginx container depending on your platform. You’re correct to follow best practice and keep the two containers separate and not run everything in one container.
Thank you for your reply! I feel like the app and the code-runner (php) would fit better together and separate the webserver from them. That makes sense and is indeed how my intuition told me, but now I need to see how I can make a folder of a container (e.g. /code) avalable to the nginx? I guess it would be something like
volumes:
for php-fpm and
volumes:
for the nginx part - or something like that? Normally left part is the host path and right after: is the path mapped in the container. In this case we do not want to map anything from the host into the container...right? I feel it's just the syntax. When this is done and understood, I will soon get into swarm/k8s :)
Edit: forgot to mention this runs on a single ec2 instance for now which already works with frontproxy, ssl, registry and all that stuff, which is just plain awesome. The example scenario is one of 3 virtual hosts (the mentioned compose represents one of them, like a substack if I may call it that), I will adapt the solution we are working on here, just unable to find a good example you know...
Yes, that’s what you would do.
If you’re just running this in production with Docker compose then you can use the same syntax there. Depending on what syntax version you’re using, you will have to used named volumes with v3. There are lots of examples of both versions solving the same problem here: https://github.com/docker/compose/issues/4379
Forgot to mention I'm using v2 for now but should not be a problem to use v3. I saw a volume definition once that omitted the host part (left) and thought that might fit for my situation, and as you say this should be the way to go! so I will give it a go first thing in the morning (way past 3am here now) One slightly related question: I always wonder if a host volume mount (e.g. host machine path mapped into container) has any negative effects on performance when 2 or more containers try to access the same files at the same time - is there any problem about locking or something?
Update: it worked perfectly! I rebuild the image of php-fpm and included my webapp in /code, then I mapped this folder as volume in the compose file without the host-side (left) and for the nginx part i added volumes_from "php-fpm". The code is taken from the container, I exec'd into it to make sure (no volumes from host machine mounted anymore). This also goes foe the config files which are now directly added to the image. Very nice! I will post the files here when I get home so others can see how to set this up. Thanks for your help!
I'm in the same sitation as yours, so if you could share those file that would be great.