Search code examples
dockerdocker-composegoogle-compute-enginejwilder-nginx-proxy

How to setup nginx-proxy to choose right container without needing the port number


Im trying to setup an nginx proxy on a Google Compute VM. So I'd like my domain name (www.example.com) to goto the right container. But for some reason, it only works if I put the port number in the address bar (www.example.com:3001), which totally defeats the purpose.

Can anyone let me know what I'm doing wrong??

nginx-proxy docker-compose.yml

version: '3'
services:
  nginx-proxy:
    image: jwilder/nginx-proxy
    ports:
      - 80:80
    volumes:
      - /var/run/docker.sock:/tmp/docker.sock:ro
    networks:
      - backend
networks:
  backend:
    driver: bridge

app docker-compose.yml

version: '3'
services:
  api:
    image: api-image:latest
    build:
      context: ./
      dockerfile: Dockerfile
    ports:
      - 3000:8080
    networks:
      - backend
  public:
    image: app-image:latest
    build:
      context: ./frontend
      dockerfile: Dockerfile
    ports:
      - 3001:8081
    networks:
      - backend
    environment:
      - VIRTUAL_HOST=www.example.com
      - VIRTUAL_PORT=3001
networks:
  backend:
    driver: bridge

So the above correctly proxies to the public container when I put www.example.com:3001.

Ideas?? Thanks!


Solution

  • You have defined this block in 2 files.

    networks:
      backend:
        driver: bridge
    

    Seemingly it's the same network but they are not. If you docker network ls you will see 2 networks with their project names as prefix.

    Change it to:

    networks:
      backend:
        external: true
    

    And create the network manually with docker network create backend