Docker: Multiple php-fpm containers on same port, different networks

I am trying to create two containers from the same image, each running on its own network, but listening on the same port. I would like to be able to reach either of them (myservice or myservice-test) from my nginx container.

Here is my (simplified) docker-compose.yml file:

version: '3.7'

services:
  myservice:
    image: php:8.0-fpm
    container_name: myservice
    networks:
      - myservice.local

  myservice-test:
    image: php:8.0-fpm
    container_name: myservice-test
    networks:
      - myservice.test

  nginx:
    image: nginx:latest
    container_name: nginx
    restart: always
    ports:
      - "80:80"
    volumes:
      - ./nginx/templates:/etc/nginx/templates:rw,cached
    networks:
      - myservice.local
      - myservice.test

networks:
  myservice.local:
  myservice.test:

The php:8.0-fpm image by default listens on 0.0.0.0:9000 and is reachable from my nginx container with this configuration.
However, the problem is that both myservice:9000 and myservice-test:9000 end up at myservice! It is impossible to reach myservice-test on port 9000!

This is the upstream config I am using in my nginx container:

upstream php {
    server myservice:9000;
}
upstream php-test {
    server myservice-test:9000;
}

When I use fastcgi_pass php-test; the requests end up at myservice, not myservice-test! … WHY?!? What am I missing? They are on different networks entirely, how are their ports still conflicting?

Source: Docker Questions

LEAVE A COMMENT