5

I have a Caddy server running in Docker talking to a Node.JS server. This setup currently works on MacOS, but does not work on WSL2. I assume the issue has something to do with the fact that we're using http://host.docker.internal:3000 as the proxy address in the Caddyfile, but I don't know of a way to write it so it works on WSL2 and in MacOS.

docker-compose.yml:

version: '3.7'
services:
  caddy:
    image: 'abiosoft/caddy:latest'
    volumes:
      - ./certs:/root/certs # to sync mkcert certificates to Caddy
      - ./Caddyfile:/etc/Caddyfile # to mount custom Caddyfile
    ports:
      - '443:2015'
  db:
    container_name: service_local_db
    image: mysql:8.0
    environment:
      MYSQL_DATABASE: 'service_local'
      MYSQL_ROOT_PASSWORD: '******'
    ports:
      - '3306:3306'
    expose:
      - '3306'
    volumes:
      - database_volume:/var/lib/mysql
volumes:
  database_volume:

Caddyfile

servicename.url{
    log stdout
    tls /root/certs/servicename.local.pem /root/certs/servicename.local-key.pem

    proxy / http://host.docker.internal:3000 {
        websocket
        transparent
        header_upstream X-Marotagem  true
        header_upstream Host  "servicename.local"
    }
}

I have tried:

  • Changing host.docker.internal to host-gateway. Even if that did work, it would inversely not allow it to work on MacOS.
  • Adding 'host.docker.internal:host-gateway' as extra_hosts: under services in the docker-compose.yml. It did not work, but if it did I am not sure how it would affect MacOS.

Any help would be appreciated.

loganhuskins
  • 1,399
  • 3
  • 15
  • 33
  • Wjy don't you just add the nodejs as a service to the services in this compose file so caddy can reach it on `http://mynodejsservice` ? – zsolt Feb 21 '21 at 16:36

1 Answers1

3

AFAIK host.docker.internal is not (yet?) implemented in Docker for Linux. But since you are using a bridge network (the default one), you can make something like a static IP-address for the host. There will be no need to use host.docker.internal after that, though if you like, you will be able to add it to a container with extra_hosts.

version: "2"
networks:
  default:
    ipam:
      driver: default
      config:
          # (mandatory) IP-address range for the containers
        - subnet: "10.50.0.0/24"
          # (optional) IP-address of the host
          # if not specified it will be the first IP-address of the subnet (10.50.0.1 in this case)
          gateway: 10.50.0.20
          # 'gateway' is only available in docker-compose version 2 at the moment

In this example gateway will be a host machine IP-address for containers in that network. You can use this value to create a working extra_hosts record:

extra_hosts:
- "host.docker.internal:10.50.0.20"

Unfortunately, gateway option is only supported in version 2 compose file specification at the moment, with version 3 you can specify only subnet. If gateway is not specified explicitly, it will be the first IP-address of the range (10.50.0.1 for the example above).

The configuration would not require changes, unless you would stumble into IP range overlapping. In other words, if the machine(s) where you will be running this would have no subnets (docker or other), overlapping with the range you've selected, there will be no problem. Otherwise you can pick another subnet and write a different address in extra_hosts.

Also note that changes to IPAM configuration are not permitted once a network has been created. You need to delete the old network before creating a new one. Use docker-compose down or docker network rm <network_name>.

anemyte
  • 17,618
  • 1
  • 24
  • 45
  • Good info, thanks. I have added the `networks` block to my docker-compose.yml. We're on version 3.7, so I could not specify the subnet. I also changed my my Caddyfile to `proxy / http://10.50.0.1:3000`, the first IP on that subnet. I am still having the same issue. Any thoughts? – loganhuskins Feb 18 '21 at 22:10
  • @loganhuskins run `docker network inspect `, look for IPAM.Config. Do you see the expected values there? Also check there if network driver is `bridge` (don't confuse with IPAM driver, you need a top-level key). – anemyte Feb 19 '21 at 05:53
  • I see two IPs, `10.50.0.3/24` for Caddy, and `10.50.0.4/24` for the database. The Node.js application isn't running in Docker, it's only being proxied through Docker, so it's not on that list. I have both of `.1` and `.2` in the proxy line in the Caddyfile and neither worked. – loganhuskins Feb 19 '21 at 14:08
  • @loganhuskins Does the node.js server listen for `0.0.0.0` or just localhost? Also try ping the host from inside the container. – anemyte Feb 19 '21 at 16:48