Use haproxy container to redirect to another container not in the same docker and with not the same port

Hello everyone,

Can you help me please ?

I have 2 docker-compose one with various app and haproxy

services:


   haproxy:
       image: haproxy:2.8
       container_name: haproxy
       ports:
           - 80:80
           - 443:443
       expose:
           - '80'
           - '443'
       volumes:
           - ./config/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
           - ./config/haproxy/certs/:/usr/local/etc/haproxy/certs/
           - ../log:/var/log
       depends_on:
           - apache-php56
           - apache-php74
           - apache-php82
       networks:
           network:
               ipv4_address: 10.10.0.2


   apache-php56:
       user: root
       container_name: php56
       image: permisapoints/apache-php56
       depends_on:
           - mariadb
       volumes:
           - ./config/php56/sites-custom:/etc/apache2/sites-custom
           - ./config/php56/sites-enabled:/etc/apache2/sites-enabled
           - ./config/php56/hosts:/etc/hosts
           - ../sites:/var/www/sites
           - ../data:/data
           - ../log:/var/log
       networks:
           network:
               ipv4_address: 10.10.0.3


   apache-php74:
       user: root
       container_name: php74
       image: permisapoints/apache-php74
       depends_on:
           - mariadb
       volumes:
           - ./config/php74/sites-custom:/etc/apache2/sites-custom
           - ./config/php74/sites-enabled:/etc/apache2/sites-enabled
           - ./config/php74/hosts:/etc/hosts
           - ../sites:/var/www/sites
           - ../data:/data
           - ../log:/var/log
       networks:
           network:
               ipv4_address: 10.10.0.4


   apache-php82:
       user: root
       container_name: php82
       image: permisapoints/apache-php82
       depends_on:
           - mariadb
       volumes:
           - ./config/php82/sites-custom:/etc/apache2/sites-custom
           - ./config/php82/sites-enabled:/etc/apache2/sites-enabled
           - ./config/php82/hosts:/etc/hosts
           - ../sites:/var/www/sites
           - ../data:/data
           - ../log:/var/log
       networks:
           network:
               ipv4_address: 10.10.0.5


   mariadb:
       container_name: mariadb
       image: permisapoints/mariadb
       ports:
           - 3306:3306
       restart: always
       volumes:
           - ./database:/var/lib/mysql
           - ../log/mysql:/var/log/mysql
           - data:/nfs
       networks:
           network:
               ipv4_address: 10.10.0.6


   memcached:
       container_name: memcached
       image: memcached
       networks:
           network:
               ipv4_address: 10.10.0.7


   redis:
       container_name: redis
       image: redis
       networks:
           network:
               ipv4_address: 10.10.0.8


   redis-commander:
       container_name: redis-commander
       hostname: redis-commander
       image: rediscommander/redis-commander:latest
       restart: always
       environment:
           - REDIS_HOSTS=local:redis:6379
       ports:
           - "8081:8081"
       networks:
           network:
               ipv4_address: 10.10.0.9


   rabbitmq:
       container_name: rabbitmq38
       image: permisapoints/rabbitmq
       ports:
           - 15672:15672
       networks:
           network:
               ipv4_address: 10.10.0.10


   tor:
       container_name: tor
       image: dockage/tor-privoxy
       ports:
           - 9050:9050
           - 9051:9051
           - 8118:8118
       networks:
           network:
               ipv4_address: 10.10.0.11


volumes:
   data:
       driver: local
       driver_opts:
           type: cifs
           device: "//10.75.10.11/servers/srv-hubic/pap/mysql"
           o: "username=zamp,password=PrgAmp,uid=1000,gid=1000"


networks:
 network:
   name: pap_dev_network
   external: true
   driver: bridge
   ipam:
     driver: default
     config:
       - subnet: 10.10.0.0/16
         gateway: 10.10.0.1

And an other app with this own docker with caddy (http server) and php app GitHub - api-platform/api-platform: 🕸️ Create REST and GraphQL APIs, scaffold Jamstack webapps, stream changes in real-time.


services:
 php:
   container_name: 'api-gateway'
   image: ${IMAGES_PREFIX:-}app-php
   restart: unless-stopped
   environment:
     SERVER_NAME: ${SERVER_NAME:-api-gateway.localhost}, php:80
     TRUSTED_PROXIES: ${TRUSTED_PROXIES:-127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16}
     TRUSTED_HOSTS: ${TRUSTED_HOSTS:-^${SERVER_NAME:-example\.com|api-gateway\.localhost}|php$$}
   volumes:
     - caddy_data:/data
     - caddy_config:/config
   networks:
     pap_dev_network:
       ipv4_address: 10.10.0.12
   expose:
     - "85"
   ports:
     # HTTP
     - target: 80
       published: ${HTTP_PORT:-85}
       protocol: tcp
     # # HTTPS
     # - target: 443
     #   published: ${HTTPS_PORT:-450}
     #   protocol: tcp
     # # HTTP/3
     # - target: 443
     #   published: ${HTTP3_PORT:-443}
       # protocol: udp


networks:
 pap_dev_network:
   external: true
volumes:
 caddy_data:
 caddy_config:

The caddy file

{
   {$CADDY_GLOBAL_OPTIONS}


   frankenphp {
       {$FRANKENPHP_CONFIG}
   }


   auto_https disable_redirects
}


{$CADDY_EXTRA_CONFIG}


http://api-gateway.localhost {
   bind 0.0.0.0


   tls internal
  
   respond "Hello, world! I am being accessed from {scheme}."
}

Haproxy conf

global
   log stdout format raw local0
   maxconn 2000
   daemon
defaults
   log global
   mode http
   option httplog
   option dontlognull
   retries 3
   timeout connect 5s  # Timeout de connexion de 5 secondes
   timeout client 500s  # Timeout client de 500 secondes
   timeout server 500s  # Timeout serveur de 500 secondes


resolvers docker
   nameserver dns 127.0.0.11:53
   resolve_retries 3
   timeout resolve 1s
   timeout retry   1s
   hold other      10s
   hold refused    10s
   hold nx         10s
   hold timeout    10s

frontend http-in


   bind *:80


   acl is_gateway hdr(host) -i api-gateway.localhost
   use_backend caddy_backend if is_gateway


backend php56_backend
   server apache-php56 apache-php56:80


backend php74_backend
   server apache-php74 apache-php74:80


backend php82_backend
   server apache-php82 apache-php82:80


backend caddy_backend
  server php 127.0.0.1:85 check

The problem is i have a host my macos with 127.0.0.1 api-gateway.localhost
If i access http://api-gateway.localhost:85 my route work
But if i use without port i have an error

<body>
   <h1>503 Service Unavailable</h1>
   No server is available to handle this request.
</body>


</html>

How to do to redirect traffic from ha proxy i the container to caddy in other container ?
Is possible to use https ?

Thanks

I find the solution

backend caddy_backend
server api-gateway api-gateway:80 check

and expose port 80

    expose:
      - "80"
    ports:
      # HTTP
      - target: 80
        published: ${HTTP_POR

To run multiple services on the same port, you need a reverse proxy. It’s easiest to use a proxy that configures automatically depending on Docker services/containers running.

We run nginx-proxy with companion with automatic config via env vars (single node Docker) or Traefik (example) with automatic config via labels (single node and Docker Swarm).

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.