title: How to configure Caddy 2 in docker-compose file to do reverse proxy without a bind mounted Caddyfile?
body: <p>I have a docker-compose file with two services:</p>
  
  <ul>
  <li>my webapp, it exposes port 3000</li>
  <li>caddy, it works as reverse proxy for my web app and gives HTTPS</li>
  </ul>
  
  <p>It works fine if I use a bind mounted Caddyfile:</p>
  
  <pre><code>caddy:
      image: caddy:2.0.0-alpine
      ports:
        - 80:80
        - 443:443
      volumes:
        - caddy:/data
        - .Caddyfile:/etc/caddy/Caddyfile
  </code></pre>
  
  <p>Caddy file is pretty simple:</p>
  
  <pre><code>my-domain.com {
      reverse_proxy my-app:3000
  }
  
  </code></pre>
  
  <p>But I'd like to deploy it on the server without uploading Caddyfile. I want to configure my docker-compose.yml something like this:</p>
  
  <pre><code>version: "3"
  
  services:
    my-app:
      image: my-app
      expose:
        - 3000
  
    caddy:
      image: caddy:2
      ports:
        - 80:80
        - 443:443
      environment:
        - reverse_proxy_from=my-app:3000
        - reverse_proxy_to=my-domain.com
      volumes:
        - caddy:/data
  
  
  volumes:
    caddy:
  
  </code></pre>
  
  <p>Also maybe it's possible to do it via caddy API. When I tried to configure a reversy proxy on bare OS, it works. But when I tried to do it with docker (<code>docker-compose exec caddy caddy reverse-proxy --from my-site.net --to my-app:3000</code>), I got some unclear to me errors messages:</p>
  
  <pre><code>root@test:/xxx# docker-compose exec caddy caddy reverse-proxy --from my-site.net --to app:3000
  2020/05/15 11:49:57.787 WARN    admin   admin endpoint disabled
  2020/05/15 11:49:57.787 INFO    http    server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS {"server_name": "proxy", "https_port": 443}
  2020/05/15 11:49:57.787 INFO    http    enabling automatic HTTP-&gt;HTTPS redirects    {"server_name": "proxy"}
  2020/05/15 11:49:57 [INFO][cache:0xc000726820] Started certificate maintenance routine
  2020/05/15 11:49:57.788 INFO    tls cleaned up storage units
  reverse-proxy: loading new config: http app module: start: tcp: listening on :80: listen tcp :80: bind: address already in use
  </code></pre>