Search code examples
phpdockernginxfpm

How to make self-contained nginx and php-fpm containers properly


I have two docker-containers: nginx and php-fpm.

I want to make them self-contained (containers will clone repo from git on build) for production. But after cloning repo I need to make some initial stuff, like composer install in project folder. I can do it inside php-fpm container, because I have php there. But how to prepare code in nginx container? I don't have php there for composer.

Everything is fine, when I mount same initialized folder into both containers.

Maybe I'm doing something wrong, what is best way to do self-contained container for nginx+php-fpm?

For now I have this nginx-config:

server {
    listen       80;
    server_name  localhost;

    index index.php index.html;

    # I need only /api/ path
    location /api/ {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {      
        root /var/www/public;
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass api:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

Solution

  • You should only have the PHP files on the FPM server. As you previously noted, you should have php cli to run composer on that server. You shouldn't need any PHP files on the nginx server. SCRIPT_FILENAME will be resolved by the CGI server so that location does not need to exist on the web proxy. If you need to make config changes to the nginx server, you probably want to use something more system oriented like Salt, Chef or Puppet.

    location ~ \.php$ {  
        # This is the webserver root for static junk
        root /var/www/public;
         ...
        # Point this somewhere else if the docroot is in a different location on the FPM server.
        fastcgi_param SCRIPT_FILENAME /home/php-fpm/wwwroot/$fastcgi_script_name;
    }