Skip to content

Commit 4719d87

Browse files
committed
Support --add-host with defaults for linux
Add a new `--add-host` multi flag to the `kl network start` command which allows configuring additional hosts on the proxy container. In addition to this, on linux, the `host.docker.internal:172.17.0.1` host is added to be consistent with macos.
1 parent 0acf481 commit 4719d87

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
lines changed

README.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Now you can start the docker network and proxy containers:
9595
kl network start
9696
```
9797

98-
> [!NOTE]
98+
> [!NOTE]
9999
> 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=<custom-port>`. You will then also need to update your `/etc/systemd/resolved.conf` file to match.
100100
101101
---
@@ -208,18 +208,16 @@ Below are some common ways of addressing services running in different contexts:
208208
+ `http://example:8080`
209209
+ `http://<container-ip>:8080`
210210
+ A process running on the host and bound to port `8080` could be addressed as
211-
+ On macos - `http://host.docker.internal:8080`
211+
+ On macos and linux* - `http://host.docker.internal:8080`
212212
+ On linux - `http://172.17.0.1:8080`
213213

214-
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:
215-
216-
```bash
217-
172.17.0.1 host.docker.internal
218-
```
219-
220-
**DO NOT** do this if you are on macos.
221-
222-
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.
214+
> [!NOTE]
215+
>
216+
> On **linux\*** docker does not configure the `host.docker.internal` domain which is typically only available on macos when using something like Docker Desktop.
217+
>
218+
> 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.
219+
>
220+
> 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 ':').
223221
224222
## Routes
225223

src/k16/kl/commands/network.clj

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,38 @@
3131

3232
module))
3333

34-
(defn- start-network! [{:keys [host-dns host-dns-port]}]
34+
(def ^:private os
35+
(-> (System/getProperty "os.name") .toLowerCase))
36+
37+
(def ^:private default-hosts
38+
(cond
39+
(str/includes? os "linux") ["host.docker.internal:172.17.0.1"]
40+
:else []))
41+
42+
(defn- start-network! [{:keys [host-dns host-dns-port add-host]}]
3543
(let [workdir (api.fs/from-config-dir ".kl/network")
44+
45+
extra-hosts (->> (concat default-hosts add-host)
46+
(reduce (fn [acc host]
47+
(let [[host ip] (str/split host #":")]
48+
(assoc acc host ip)))
49+
{})
50+
(filter (fn [[_ ip]]
51+
(and ip (not= "" ip))))
52+
(map (fn [[host ip]] (str host ":" ip))))
53+
3654
module (cond-> (write-network-module)
3755
(not host-dns)
3856
(assoc-in [:containers :dnsmasq-external :enabled]
3957
false)
4058

4159
host-dns-port
4260
(assoc-in [:containers :dnsmasq-external :ports]
43-
[(str host-dns-port ":53/udp") (str host-dns-port ":53/tcp")]))]
61+
[(str host-dns-port ":53/udp") (str host-dns-port ":53/tcp")])
62+
63+
(seq extra-hosts)
64+
(assoc-in [:containers :proxy :extra_hosts]
65+
extra-hosts))]
4466

4567
(api.proxy/write-proxy-config! {:module-name "kl"
4668
:module module})
@@ -61,11 +83,10 @@
6183
(api.fs/rm-dir! workdir)))
6284

6385
(def ^:private default-port
64-
(let [os (-> (System/getProperty "os.name") .toLowerCase)]
65-
(cond
66-
(str/includes? os "mac") "53"
67-
(str/includes? os "linux") "5343"
68-
:else "5343")))
86+
(cond
87+
(str/includes? os "mac") "53"
88+
(str/includes? os "linux") "5343"
89+
:else "5343"))
6990

7091
(def cmd
7192
{:command "network"
@@ -79,6 +100,9 @@
79100
:type :with-flag}
80101
{:option "host-dns-port"
81102
:default default-port
103+
:type :string}
104+
{:option "add-host"
105+
:multiple true
82106
:type :string}]
83107

84108
:runs start-network!}

0 commit comments

Comments
 (0)