Skip to content

Commit cae5916

Browse files
committed
docs: fix --add-host example to run Python server in background
Update the --add-host example to show how to run the Python HTTP server in the background and stop it afterwards. This ensures the example is functional in a single terminal session.
1 parent 694d1a0 commit cae5916

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

docs/reference/commandline/container_run.md

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,15 +1290,33 @@ The following example shows how the special `host-gateway` value works. The
12901290
example runs an HTTP server that serves a file from host to container over the
12911291
`host.docker.internal` hostname, which resolves to the host's internal IP.
12921292

1293-
```console
1294-
$ echo "hello from host!" > ./hello
1295-
$ python3 -m http.server 8000
1296-
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...
1297-
$ docker run \
1298-
--add-host host.docker.internal=host-gateway \
1299-
curlimages/curl -s host.docker.internal:8000/hello
1300-
hello from host!
1301-
```
1293+
1. Create a file named `hello`:
1294+
1295+
```console
1296+
$ echo "hello from host!" > ./hello
1297+
```
1298+
1299+
2. Run an HTTP server in the background:
1300+
1301+
```console
1302+
$ python3 -m http.server 8000 &
1303+
[1] 12345
1304+
```
1305+
1306+
3. Run a container that accesses the file on the host:
1307+
1308+
```console
1309+
$ docker run \
1310+
--add-host host.docker.internal=host-gateway \
1311+
curlimages/curl -s host.docker.internal:8000/hello
1312+
hello from host!
1313+
```
1314+
1315+
4. Stop the HTTP server:
1316+
1317+
```console
1318+
$ kill $!
1319+
```
13021320

13031321
The `--add-host` flag also accepts a `:` separator, for example:
13041322

0 commit comments

Comments
 (0)