lucaslorentz / caddy-docker-proxy

Caddy as a reverse proxy for Docker
MIT License
2.86k stars 168 forks source link

Change default ports 80 and 443 #653

Closed jdeg closed 1 month ago

jdeg commented 1 month ago

Issue: Changing Default Caddy Ports for Serving Local Drupal Environments

I have an application Dresktop that creates local Drupal environments. I use Caddy to serve over HTTPS and provide friendly URLs. I want to enable users to configure alternative Caddy ports in case ports 80 and 443 are already in use.

To test this, I tried running a httpd container on port 80 and configured Caddy to use alternative ports. Here's my setup:

services:
  caddy:
    image: lucaslorentz/caddy-docker-proxy:ci-alpine
    container_name: dresktop-caddy
    ports:
      - "59707:80"
      - "59708:443"
      - "59709:8025"
      - "59710:1025"
    environment:
      - CADDY_INGRESS_NETWORKS=dresktop_network_external
    networks:
      - dresktop_network_external
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - caddy_data:/data
      - ./Caddyfile:/etc/caddy/Caddyfile
    restart: unless-stopped
networks:
  dresktop_network_external:
    external: true
volumes:
  caddy_data: {}

and this is docker compose configuration file

services:
  drupal:
    image: jdeg/dresktop:latest
    container_name: dev.website-review.drt
    volumes:
      - /Projects/websiteReview/website:/opt/drupal
    restart: always
    networks:
      - dresktop_network_external
      - dresktop_network_internal
    labels:
      dresktop: true
      caddy: dev.website-review.drt.localhost
      caddy.reverse_proxy: "{{ upstreams 59707 }}"
      caddy.tls: internal
  database:
    image: mariadb
    container_name: db.dev.website-review.drt
    restart: always
    environment:
      MARIADB_ROOT_PASSWORD: drupal
      MARIADB_DATABASE: drupal
      MARIADB_USER: drupal
      MARIADB_PASSWORD: drupal
      MARIADB_TRANSACTION_ISOLATION: READ-COMMITTED
    networks:
      - dresktop_network_internal
networks:
  dresktop_network_external:
    external: true
  dresktop_network_internal:
    internal: true

The idea is to use dev.website-review.drt.localhost and access the Drupal environment, but I get this when I test it wih Curl

curl https://dev.website-review.drt.localhost/
curl: (7) Failed to connect to dev.website-review.drt.localhost port 443 after 0 ms: Couldn't connect to server

I’ve tried several Caddyfile configurations but keep encountering the same issue. I'm not sure if the issue lies in the Caddyfile, the Docker Compose setup, or something else.

{
    http_port 59707
    https_port 59708
}

dev.website-review.drt.localhost {
    reverse_proxy dev.website-review.drt:80
    tls internal
}

Is there something I’m missing in my configuration to successfully change the Caddy ports and access the Drupal environment using the alternative ports? Thanks in advance.

francislavoie commented 1 month ago

If you change the port you publish on the host, then you have to use that port when you make a request. Do curl -v https://dev.website-review.drt.localhost:59708/

Keep in mind that if you use non-standard ports, the ACME HTTP and TLS-ALPN challenges will not work (you must use ports 80 and 443 for those challenges) so you wouldn't be able to get publicly trusted TLS certs.

jdeg commented 1 month ago

Perfect, thank you very much.