Skip to content

Commit 36c65f7

Browse files
Support host network_mode
1 parent 9e56f9f commit 36c65f7

File tree

2 files changed

+49
-16
lines changed

2 files changed

+49
-16
lines changed

prompts/chrome.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ tools:
2323
- websocat_args
2424
container:
2525
image: vonwig/websocat:latest
26+
network_mode: host
2627
stdin:
2728
content: "{{message|safe}}"
2829
command:
@@ -38,6 +39,7 @@ tools:
3839
description: The arguments to pass to curl
3940
container:
4041
image: vonwig/curl:latest
42+
network_mode: host
4143
command:
4244
- "{{raw|safe}}"
4345
- name: chrome
@@ -52,9 +54,8 @@ tools:
5254
- url
5355
container:
5456
image: zenika/alpine-chrome
57+
network_mode: host
5558
background: true
56-
ports:
57-
- "9222:9222"
5859
command:
5960
- "--no-sandbox"
6061
- "--remote-debugging-address=0.0.0.0"
@@ -81,13 +82,13 @@ Examples:
8182

8283
```sh
8384
# Get the websocket url
84-
curl -X PUT -H "HOST: localhost:9222" -sg http://host.docker.internal:9222/json/new
85+
curl -X PUT -sg http://localhost:9222/json/new
8586

8687
# Navigate to a page
8788

8889
$MESSAGE='Page.navigate {"url":"https://www.docker.com"}' # This format works with --jsonrpc where the first word is the method name and the rest is the arguments.
8990

90-
$MESSAGE | websocat -n1 --jsonrpc --jsonrpc-omit-jsonrpc ws://host.docker.internal:9222/devtools/page/<PAGE_ID>
91+
$MESSAGE | websocat -n1 --jsonrpc --jsonrpc-omit-jsonrpc ws://localhost:9222/devtools/page/<PAGE_ID>
9192

9293
{"id":2,"result":{"frameId":"A331E56CCB8615EB4FCB720425A82259","loaderId":"EF5AAD19F2F8BB27FAF55F94FFB27DF9"}}
9394
```

src/docker.clj

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,44 @@
105105
:throw false
106106
:as :stream}))
107107

108+
(defn parse-port [s]
109+
(let [[_ host container] (re-find #"(.*):(.*)" s)]
110+
{:host {:port host}
111+
:container {:port container
112+
:protocol "tcp"}}))
113+
114+
(defn exposed-ports
115+
" params
116+
- coll of strings (e.g. [\"9222:9222\"])"
117+
[coll]
118+
(->> (for [s coll :let [{:keys [container]} (parse-port s)]]
119+
[(format "%s/%s" (:port container) (:protocol container)) {}])
120+
(into {})))
121+
122+
(defn port-bindings
123+
" params
124+
- coll of strings (e.g. [\"9222:9222\"])"
125+
[coll]
126+
(->>
127+
(for [s coll :let [{:keys [container host]} (parse-port s)]]
128+
[(format "%s/%s" (:port container) (:protocol container)) [{:HostPort (:port host)}]])
129+
(into {})))
130+
131+
(comment
132+
(port-bindings ["9222:9222"])
133+
(exposed-ports ["9222:9222"]))
134+
108135
;; check for 201
109136
;; entrypoint is an array of strings
110137
;; env is a map
111138
;; Env is an array of name=value strings
139+
;;
140+
;; opts
141+
;;
112142
;; Tty wraps the process in a pseudo terminal
113-
{:StdinOnce true
114-
:OpenStdin true}
115-
(defn create-container [{:keys [image entrypoint workdir command host-dir env thread-id opts mounts volumes ports]
143+
;; StdinOnce closes the stdin after the first client detaches
144+
;; OpenStdin just opens stdin
145+
(defn create-container [{:keys [image entrypoint workdir command host-dir env thread-id opts mounts volumes ports network_mode]
116146
:or {opts {:Tty true}}}]
117147
(let [payload (json/generate-string
118148
(merge
@@ -123,16 +153,18 @@
123153
(into []))})
124154
{:HostConfig
125155
(merge
126-
{:Binds
127-
(concat ["docker-lsp:/docker-lsp"
128-
"/var/run/docker.sock:/var/run/docker.sock"]
129-
(when host-dir [(format "%s:/project:rw" host-dir)])
130-
(when thread-id [(format "%s:/thread:rw" thread-id)])
131-
(or volumes mounts))}
132-
(when ports
133-
{:PortBindings {"9222/tcp" [{:HostPort "9222"}]}}))
156+
{:Binds
157+
(concat ["docker-lsp:/docker-lsp"
158+
"/var/run/docker.sock:/var/run/docker.sock"]
159+
(when host-dir [(format "%s:/project:rw" host-dir)])
160+
(when thread-id [(format "%s:/thread:rw" thread-id)])
161+
(or volumes mounts))}
162+
(when network_mode
163+
{:NetworkMode network_mode})
164+
(when ports
165+
{:PortBindings (port-bindings ports)}))
134166
:WorkingDir (or workdir "/project")}
135-
(when ports {:ExposedPorts {"9222/tcp" {}}})
167+
(when ports {:ExposedPorts (exposed-ports ports)})
136168
(when entrypoint {:Entrypoint entrypoint})
137169
(when command {:Cmd command})))
138170
ascii-payload (String. (.getBytes payload "ASCII"))]

0 commit comments

Comments
 (0)