Skip to content

Commit

Permalink
Add support for host.docker.internal on linux
Browse files Browse the repository at this point in the history
  • Loading branch information
julienvincent committed Nov 22, 2023
1 parent 0acf481 commit 64726dd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
15 changes: 5 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,13 @@ Below are some common ways of addressing services running in different contexts:
+ `http://example:8080`
+ `http://<container-ip>: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]
> By default docker does not configure the `host.docker.internal` domain on linux*. This 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:"`.
## Routes

Expand Down
38 changes: 31 additions & 7 deletions src/k16/kl/commands/network.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,38 @@

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]
false)

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})
Expand All @@ -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"
Expand All @@ -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!}
Expand Down

0 comments on commit 64726dd

Please sign in to comment.