diff --git a/README.md b/README.md index 7ba9131..a25cfe9 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,7 @@ Now you can start the docker network and proxy containers: kl network start ``` -> [!NOTE] +> [!NOTE] > On linux the host DNS network container's port defaults to binding to `5343`. If you would like to change this you can start the networking components with a different port by running `kl network start --host-dns-port=`. You will then also need to update your `/etc/systemd/resolved.conf` file to match. --- @@ -208,18 +208,16 @@ Below are some common ways of addressing services running in different contexts: + `http://example:8080` + `http://:8080` + A process running on the host and bound to port `8080` could be addressed as - + On macos - `http://host.docker.internal:8080` + + On macos and linux* - `http://host.docker.internal:8080` + On linux - `http://172.17.0.1:8080` -Because of this host address discrepancy between linux and macos it is highly recommended for **linux based users** to add the following to their `/etc/hosts` file: - -```bash -172.17.0.1 host.docker.internal -``` - -**DO NOT** do this if you are on macos. - -This is an acceptable compromise to allow for a stable way of addressing the host in module configs that are intended to be consumed by developers on different operating systems. +> [!NOTE] +> +> On **linux\*** docker does not configure the `host.docker.internal` domain which is typically only available on macos when using something like Docker Desktop. +> +> To allow for a consistent way of addressing the host that works across all operating systems kl manually adds the `host.docker.internal:172.17.0.1` host to to the proxy container. +> +> If you don't want this behaviour you can disable it by running `kl network start --add-host "host.docker.internal:"` (note the empty ip after the ':'). ## Routes diff --git a/src/k16/kl/commands/network.clj b/src/k16/kl/commands/network.clj index 2999f5e..3248d41 100644 --- a/src/k16/kl/commands/network.clj +++ b/src/k16/kl/commands/network.clj @@ -31,8 +31,26 @@ module)) -(defn- start-network! [{:keys [host-dns host-dns-port]}] +(def ^:private os + (-> (System/getProperty "os.name") .toLowerCase)) + +(def ^:private default-hosts + (cond + (str/includes? os "linux") ["host.docker.internal:172.17.0.1"] + :else [])) + +(defn- start-network! [{:keys [host-dns host-dns-port add-host]}] (let [workdir (api.fs/from-config-dir ".kl/network") + + extra-hosts (->> (concat default-hosts add-host) + (reduce (fn [acc host] + (let [[host ip] (str/split host #":")] + (assoc acc host ip))) + {}) + (filter (fn [[_ ip]] + (and ip (not= "" ip)))) + (map (fn [[host ip]] (str host ":" ip)))) + module (cond-> (write-network-module) (not host-dns) (assoc-in [:containers :dnsmasq-external :enabled] @@ -40,7 +58,11 @@ host-dns-port (assoc-in [:containers :dnsmasq-external :ports] - [(str host-dns-port ":53/udp") (str host-dns-port ":53/tcp")]))] + [(str host-dns-port ":53/udp") (str host-dns-port ":53/tcp")]) + + (seq extra-hosts) + (assoc-in [:containers :proxy :extra_hosts] + extra-hosts))] (api.proxy/write-proxy-config! {:module-name "kl" :module module}) @@ -61,11 +83,10 @@ (api.fs/rm-dir! workdir))) (def ^:private default-port - (let [os (-> (System/getProperty "os.name") .toLowerCase)] - (cond - (str/includes? os "mac") "53" - (str/includes? os "linux") "5343" - :else "5343"))) + (cond + (str/includes? os "mac") "53" + (str/includes? os "linux") "5343" + :else "5343")) (def cmd {:command "network" @@ -79,6 +100,9 @@ :type :with-flag} {:option "host-dns-port" :default default-port + :type :string} + {:option "add-host" + :multiple true :type :string}] :runs start-network!}