3

I'm currently trying to setup a PHP development environment using NGINX and PHP-FPM Docker containers.

Now I know that this can be accomplished either as a single container running both services, or even as a multiple container setup where each service runs in its own container (that's based on my research so far).

My problem can be divided into two questions:

  1. How would one setup multiple different PHP apps and have them communicate with one another, using the multiple container solution? Will I have to setup a NGINX container for every PHP-FPM one, or can I setup one NGINX container and use it with multiple different PHP-FPM ones?

  2. Is the multiple container solution even a good one for a development environment? Or am I approaching this totally wrong?

Thanks in advance, any tips would be appreciated!

2 Answers 2

3

Since, you are asking about multi container environment, I believe you are using Docker compose for your application. To answer your question more elaborately.

1) Lets say we have three containers in docker compose, one for nginx, an fpm container for laravel and another fpm container for magento. then you docker compose file would look like. The below snippet is only an example

nginx:
  image: nginx:latest
  ports:
    - "80:80"
    - "443:443"
  links:
    - "fpmlaravel"
    - "fpmmagento"
fpmlaravel:
  image: php:latest
  volumes:
    - ./data/laravel/:/var/www/laravel
  links:
    - "nginx"
fpmmagento:
  image: php:latest
  volumes:
    - ./data/magentoroot/:/var/www/magento
  links:
    - "nginx"

The nginx vhost file for magento would be

# PHP back end
upstream backend {
    server fpmmagento:9000;
}

server {

    listen 80;
    server_name www.magento.dev magento.dev;
    root /var/www/magento;

    location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass backend;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param  HTTPS    $fastcgi_https;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param SCRIPT_NAME $fastcgi_script_name;
    fastcgi_param MAGE_RUN_TYPE $mage_type;
    fastcgi_param MAGE_RUN_CODE $mage_code;
  }
}

For fpm, make sure that fpm image is listening on port 9000.

2) Yes it is possible to have one nginx container, which routes between different fpm backends, based on nginx vhosts. But, in general it is considered a bad practice, as the main idea behind docker is to segregate your applications.

Sign up to request clarification or add additional context in comments.

3 Comments

v1 syntax is considered deprecated and shouldn't be encouraged any more. I'd suggest updating your example :+1
Thank you very much for your detailed response and code examples, I'll give this a shot later on today and let you know how it went.
Worked like a charm, thank you for all the useful information!
0

Running Multiple Processes in a Single Container:

This is generally considered back practice, but we have to take into consideration that nginx and fpm need to work together. You can use shared volumes and separate them, but I'd instead rather see people use --init and just allow both processes, to remove complexity.

Should you wish to go down the multiple container route, I'd do as follows:

  1. Add a service (nginx) that has a single index.php file, which can be empty
  2. Ensure nginx passes all *.php requests to php container, using it's service name
  3. Your fpm container, called php will need all the source code, inside the same path as your nginx's indx.php

2 Comments

Thanks for the response, although I didn't quite understand if you meant this was bad practice or not, and why. Moreover, the setup you described is pretty basic and sort of disregards another part of my question which is the "multi-app" setup. Nonetheless, the information you supplied was indeed useful, I'll keep looking into it as soon as possible.
It is bad practice, unless you prepare for zombies. In fpm/nginx use-case, it's acceptable to remove any complexity. Best of luck

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.