phpdockernginxdocker-compose

Docker-Compose with PHP and Nginx not working on production


I have a very simple config in docker-compose with php:7-fpm and nginx that I want to use to host simple php websites. But I am having issues getting it available in production.

Can someone please tell me what I did wrong?

Here is docker-compose.prod.yml:

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ../company/site:/code

      - ./site.prod.conf:/etc/nginx/conf.d/default.conf

  php:
    image: php:7-fpm
    volumes:
      - ../company/site:/code

Here is the site.prod.conf file:

server {
    listen 80;
    index index.php index.html;
    server_name example.com;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /code;

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

I can compose up and the logs appear to be fine and when I run docker ps:

docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                   NAMES
c268a9cf4716   php:7-fpm      "docker-php-entrypoi…"   27 minutes ago   Up 16 seconds   9000/tcp                                example_code-php-1
beaaec39209b   nginx:latest   "/docker-entrypoint.…"   27 minutes ago   Up 16 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   example_code-web-1

Then checking the ports, I think this looks fine:

netstat -tulpn | grep :80
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      204195/docker-proxy 
tcp6       0      0 :::8080                 :::*                    LISTEN      204207/docker-proxy 

Solution

    1. You need to expose TCP port 9000 of the PHP container to made other containers able to use it (see What is the difference between docker-compose ports vs expose):
      php:
        image: php:7-fpm
        expose:
          - "9000"
        ...
    
    1. Do you really want your sites to be available on TCP port 8080, not the standard port 80? If not, change "8080:80" to "80:80".
    2. Besides the PHP handler, use a default location (although your site should be workable even without it, it is a bad practice to not add it to your nginx config):
    location / {}