Connection refused while connecting to upstream to a port other than 9000 in docker project

I’ve been struggling with running two php docker projects on the same server for a long time. For the first project, I use for the php port 9000:9000 in docker-compose.yaml. If I use the same port for another project, logically, when starting docker, it reports an error that port 9000 is already in use. Therefore, I set the port to 9002, but I get the error 502 Bad Gateway and Connection refused.

connect() failed (111: Connection refused) while connecting to upstream, client: 172.19.0.1, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://172.19.0.3:9002", host: "127.0.0.1:4443"

The first project working correctly.

Can someone advise me how to adjust the configuration or where am i doing wrong?

First docker project:

version: '3.9'

services:
  php:
    container_name: php
    build:
      context: ./docker/php
    ports:
      - '9000:9000'
    volumes:
      - .:/var/www/
      - ~/.ssh:/root/.ssh:ro
  nginx:
    container_name: nginx
    image: nginx:stable-alpine
    ports:
      - '5080:80'
      - '5443:443'
    volumes:
      - .:/var/www/
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
      - /etc/letsencrypt:/etc/letsencrypt
    depends_on:
      - php
upstream php-upstream {
    server php:9000;
}

server {
    listen 80;
    server_name localhost;

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    index index.php index.html index.htm;
    server_name localhost;
    root /var/www;
    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;

    ssl_certificate /etc/letsencrypt/live/domain1/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain1/privkey.pem;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\\.php(/|$) {
        fastcgi_pass php-upstream;
        fastcgi_split_path_info ^(.+\\.php)(/.*)$;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;

        include fastcgi_params;

        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;

        internal;
    }

    location ~ \\.php$ {
        return 404;
    }
}

Second docker project:

version: '3.9'
services:
  php:
    container_name: hostmagic_php
    build:
      context: ./docker/php
    ports:
      - '9002:9000'
    volumes:
      - .:/var/www/symfony
      - ~/.ssh:/root/.ssh:ro
  nginx:
    container_name: hostmagic_nginx
    image: nginx:stable-alpine
    ports:
      - '8080:80'
      - '4443:443'
    volumes:
      - .:/var/www/symfony
      - ./docker/nginx/default.conf:/etc/nginx/conf.d/default.conf
      - /etc/letsencrypt:/etc/letsencrypt
    depends_on:
      - php
  rabbitmq:
    image: rabbitmq:3-management-alpine
    container_name: hostmagic_rabbitmq
    environment:
      RABBITMQ_DEFAULT_USER: admin
      RABBITMQ_DEFAULT_PASS: password
      RABBITMQ_DEFAULT_VHOST: "/"
    ports:
      - 15672:15672
      - 5672:5672
upstream php-upstream {
    server php:9002;
}

server {
    listen 80;
    server_name localhost;

    location / {
        return 301 https://$host$request_uri;
    }
}

server {
    listen 443 ssl;
    listen [::]:443 ssl;
    index index.php index.html index.htm;
    server_name localhost;
    root /var/www/symfony/public;
    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;

    ssl_certificate /etc/letsencrypt/live/domain2/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/domain2/privkey.pem;

    location / {
        try_files $uri /index.php$is_args$args;
    }

    location ~ ^/index\\.php(/|$) {
        fastcgi_pass php-upstream;
        fastcgi_split_path_info ^(.+\\.php)(/.*)$;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
        fastcgi_param HTTP_HOST $host;

        include fastcgi_params;

        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 256k;
        fastcgi_busy_buffers_size 256k;

        internal;
    }

    location ~ \\.php$ {
        return 404;
    }
}

Hello,

The service within the PHP’s Docker-container is listening on port 9000.
You don’t even need the

ports:
    - '9000:9000'

because I guess you don’t need to access this container from outside your Docker-Host.
Within your nginx’s config you access the PHP-container by its service-name php (as defined in the docker-compose.yml) which is fine and is working as expected.

If you make your PHP-container accessible to the outside world on port 9002 with

ports:
    - '9002:9000'

the service within the container is still listening on port 9000 but can be accessed on your docker-host on port 9002.
Within your nginx’s config you try to access the PHP-container by its service name but with the port available on the docker-host. This will not work. You either have to access the docker-host (172.17.0.1 on linux, host.docker.internal on Windows) to access port 9002 or access the PHP-container by its service name on port 9000.

I don’t know why you would move the PHP’s container service’s port from 9000 to 9002?

1 Like

Thank you very much! It helped. I just don’t understand why most examples include docker-compose.yaml port mapping

Port mapping is required frequently, but if you mean you saw port mapping for a PHP FPM container, that I don’t know. Maybe the author wanted to use a PHP container and still use Nginx or HTTPD on the host without containers.