Nginx PHP:7-fpm 502 Bad Gateway Error

When using docker-compose and creating nginx and php services using (nginx:latest and php:7-fpm images), my application worked fine when I use php:9000 as the fastcgi_pass but when I run the 2 containers separately using docker run (full run commads below) and I set fastcgi_pass to 127.0.0.1:9000 or localhost:9000, I get 502 Bad Gateway error. I don’t understand what I’m doing wrong because I mapped port 9000 on host to port 9000 on the php container and it’s supposed to work based on my knowledge of docker.

How I started the php fpm container

docker run -p 9000:9000 --name=fpm_php -v ${PROJECT_DIR}/php:/var/www/php:rw --restart=always -d php:7-fpm

How I started the nginx container

docker run -p 80:80 -p 443:443 --name=nginx -v ${PROJECT_DIR}/logs:/var/log/nginx:rw -v ${PROJECT_DIR}/vhosts:/etc/nginx/conf.d:ro -v ${PROJECT_DIR}:/var/www:ro --restart=always -d nginx:latest

Nginx configuration for the app

    server {
    # Set the port to listen on and the server name
    listen 80;
    listen [::]:80;
    server_name icpmlw.phpapps.dev;

    # Set the document root of the project
    root /var/www/php/icpmlw.phpapps.dev/public;

    # Set the directory index files
    index index.php index.html index.htm;

    # Specify the default character set
    charset utf-8;

    # Setup the default location configuration
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # Specify the details of favicon.ico
    location = /favicon.ico { access_log off; log_not_found off; }

    # Specify the details of robots.txt
    location = /robots.txt { access_log off; log_not_found off; }

    # Specify the logging configuration
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    sendfile off;

    client_max_body_size 100m;

    # Specify what happens when PHP files are requested
    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    # Specify what happens when .ht files are requested
    location ~ /\.ht {
        deny all;
    }
}

I’m using dnsmasq to route all *.dev to localhost. Also the app’s directory structure is intact.

Operating System: Chakra
OSType: linux
Architecture: x86_64
Docker version 17.05.0-ce, build 89658be