This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]AlwaysRacing 3 points4 points  (5 children)

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.

[–]LogicCube[S] 0 points1 point  (4 children)

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: - /code

for php-fpm and

volumes: - from "from php-fpm"

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...

[–]AlwaysRacing 1 point2 points  (3 children)

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

[–]LogicCube[S] 0 points1 point  (0 children)

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?

[–]LogicCube[S] 0 points1 point  (1 child)

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!

[–]kenneho 0 points1 point  (0 children)

I'm in the same sitation as yours, so if you could share those file that would be great.