Docker + Caddy and reverse_proxy through multiple instances of docker-compose apps but via different domain

Viewed 800

i have this kind of fs:

dist app1 (docker-compose) app2 (docker-compose) app3 (docker-compose)

app1, app2, app3 are the same containerized app like:

services:
  services1:
    build:
     context: ./services1/
    env_file:
      - .env
    command: npm run start
  services2:
    build:
     context: ./services2/
    env_file:
      - .env
    command: npm run start

app1, app2, app3 are backend for different clients but they are exactly the same app, and... they run on the same port.

My problem is: How can use a Caddyfile to redirect to my backends like:

subapp1.app1.domain.io     {
        reverse_proxy app1_subapp1
}

subapp2.app1.domain.io     {
        reverse_proxy app1_subapp2
}

subapp1.app2.domain.io     {
        reverse_proxy app2_subapp1
}

subapp2.app2.domain.io     {
        reverse_proxy app2_subapp2
}

I tried to use external network approach, but... not very handy. I suppose my case is not very rare... do you have any suggestions, advice ?

Thx !

1 Answers

One simple way is to run Caddy from the same docker-compose.yml, as in:

  caddy:
    [the stuff caddy needs to run in docker]
  services1:
    [the stuff services1 needs to run in docker]
  services2:
    [the stuff services2 needs to run in docker]

That way they will all automatically be on the same Docker network. Then Caddyfile only needs the service name from docker-compose.yml, and it will figure out the ports/containers itself¹.

subapp1.app1.domain.io     {
        reverse_proxy services1
}

subapp2.app1.domain.io     {
        reverse_proxy services2
}

This is only one option. You could do multiple compose files, but then need to specify networks.

Notes:

  1. Assuming you are using a relatively recent version in 'docker-compose.yml'. The cutoff may be version '2.0', but I don't remember exactly.
Related