docker-archive / dockercloud-haproxy

HAproxy image that autoreconfigures itself when used in Docker Cloud
https://cloud.docker.com/
652 stars 181 forks source link

Ability to run this image in multiple containers on the same host using dynamic ports #161

Closed motin closed 7 years ago

motin commented 7 years ago

Please add ability to use placeholder for haproxy's published port in VIRTUAL_HOST env var.

We have a production setup that uses dockercloud-haproxy which we would like to replicate locally for local dev. However, since the VIRTUAL_HOST setting needs to include haproxy's published port, the haproxy can't currently be published on a dynamic port, meaning that we need to assign fixed ports to all separate instances of haproxy that we want to run locally. This makes it cumbersome to spin up stacks for acceptance tests which would benefit from being able to use a free dynamic port.

Here are example configs to illustrate the problem.

Working config - uses fixed port:

# Docker compose configuration for local development
version: '2'
services:

  # The local version of the public haproxy router, used to test blue/green deployments locally
  router:
    image: dockercloud/haproxy:1.6.2
    links:
      - web
    ports:
      - "8080:8080"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

  web:
    image: dockercloud/hello-world
    ports:
      - "80:80"
    environment:
      VIRTUAL_HOST: 'localhost:8080'

Not working config - uses dynamic port:

# Docker compose configuration for local development
version: '2'
services:

  # The local version of the public haproxy router, used to test blue/green deployments locally
  router:
    image: dockercloud/haproxy:1.6.2
    links:
      - web
    ports:
      - "80"
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock

  web:
    image: dockercloud/hello-world
    ports:
      - "80:80"
    environment:
      VIRTUAL_HOST: 'localhost:????' # <-- We don't know the port that haproxy will be published to

Since haproxy can be reconfigured on runtime, would it be possible to have a placeholder like %HAPROXY_PUBLIC_PORT% (or something shorter :)) in the config that is replaced with the published port of the haproxy service?

Related: https://github.com/docker/docker/issues/3778

tifayuki commented 7 years ago

@motin I don't think it is an easy job to add the dynamic port support feature. The port here is key to generate frontend , backend, ACLs, etc. We have to replace the placeholder with the actual port before running the entire script.

However, the publish port detection is sort of difficult. I don't think docker itself has the API to do it directly. What's more, the current script support three modes: legacy link mode, dockercloud mode, swarm mode. We have to write different codes for each scenario. And, personally, I've know idea how to do it.

If there are multiple VIRTUAL_HOST with different ports in the linked applications, how to map the port to the placeholder is another issue to solve.