|
| 1 | +# Host integration |
| 2 | + |
| 3 | +Bridge your container and your Mac: forward your SSH agent in, or reach a host |
| 4 | +service from inside a container. |
| 5 | + |
| 6 | +## Mount your host SSH authentication socket in your container |
| 7 | + |
| 8 | +Use the `--ssh` option to mount the macOS SSH authentication socket into your container, so that you can clone private git repositories and perform other tasks requiring passwordless SSH authentication. |
| 9 | + |
| 10 | +When you use `--ssh`, it performs the equivalent of the options `--volume "${SSH_AUTH_SOCK}:/var/host-services/ssh-auth.sock" --env SSH_AUTH_SOCK=/var/host-services/ssh-auth.sock"`. The added benefit of `--ssh` is that when you stop your container, log out, log back in, and restart your container, the system automatically updates the target path for the socket mount to the new value of `SSH_AUTH_SOCK`, so that socket forwarding continues to function. |
| 11 | + |
| 12 | +```console |
| 13 | +% container run -it --rm --ssh alpine:latest sh |
| 14 | +/ # env |
| 15 | +SHLVL=1 |
| 16 | +HOME=/root |
| 17 | +TERM=xterm |
| 18 | +PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin |
| 19 | +SSH_AUTH_SOCK=/var/host-services/ssh-auth.sock |
| 20 | +PWD=/ |
| 21 | +/ # apk add openssh-client |
| 22 | +(1/6) Installing openssh-keygen (10.0_p1-r7) |
| 23 | +(2/6) Installing ncurses-terminfo-base (6.5_p20250503-r0) |
| 24 | +(3/6) Installing libncursesw (6.5_p20250503-r0) |
| 25 | +(4/6) Installing libedit (20250104.3.1-r1) |
| 26 | +(5/6) Installing openssh-client-common (10.0_p1-r7) |
| 27 | +(6/6) Installing openssh-client-default (10.0_p1-r7) |
| 28 | +Executing busybox-1.37.0-r18.trigger |
| 29 | +OK: 12 MiB in 22 packages |
| 30 | +/ # ssh-add -l |
| 31 | +...auth key output... |
| 32 | +/ # apk add git |
| 33 | +(1/12) Installing brotli-libs (1.1.0-r2) |
| 34 | +(2/12) Installing c-ares (1.34.5-r0) |
| 35 | +(3/12) Installing libunistring (1.3-r0) |
| 36 | +(4/12) Installing libidn2 (2.3.7-r0) |
| 37 | +(5/12) Installing nghttp2-libs (1.65.0-r0) |
| 38 | +(6/12) Installing libpsl (0.21.5-r3) |
| 39 | +(7/12) Installing zstd-libs (1.5.7-r0) |
| 40 | +(8/12) Installing libcurl (8.14.1-r1) |
| 41 | +(9/12) Installing libexpat (2.7.1-r0) |
| 42 | +(10/12) Installing pcre2 (10.43-r1) |
| 43 | +(11/12) Installing git (2.49.1-r0) |
| 44 | +(12/12) Installing git-init-template (2.49.1-r0) |
| 45 | +Executing busybox-1.37.0-r18.trigger |
| 46 | +OK: 24 MiB in 34 packages |
| 47 | +/ # git clone git@github.com:some-org/some-private-repo.git |
| 48 | +Cloning into 'some-private-repo'... |
| 49 | +... |
| 50 | +``` |
| 51 | + |
| 52 | +## Access a host service from a container |
| 53 | + |
| 54 | +> [!IMPORTANT] |
| 55 | +> Due to macOS security constraints around packet filter rules, this feature has limited functionality: |
| 56 | +> - Creating a localhost domain disables Private Relay. |
| 57 | +> - The local domain packet filter rule is removed on a restart. |
| 58 | +
|
| 59 | +Create a DNS domain with `--localhost <ipv4-address>` to make a domain used by a container to access a host service. Any IPv4 address can be used as `<ipv4-address>`, which will be assigned to the domain name in container. |
| 60 | + |
| 61 | +Choose an IP address that is least likely to conflict with any networks or reserved IP addresses in your environment. Reasonably safe address ranges include: |
| 62 | + |
| 63 | +- The documentation ranges 192.0.2.0/24, 198.51.100.0/24, and 203.0.113.0/24. |
| 64 | +- The 172.16.0.0/12 private range. |
| 65 | + |
| 66 | +To connect a host HTTP server from a container, run: |
| 67 | + |
| 68 | +```bash |
| 69 | +mkdir -p /tmp/test; cd /tmp/test; echo "hello" > index.html |
| 70 | +python3 -m http.server 8000 --bind 127.0.0.1 |
| 71 | +``` |
| 72 | + |
| 73 | +Create a domain for host connection: |
| 74 | + |
| 75 | +```bash |
| 76 | +sudo container system dns create host.container.internal --localhost 203.0.113.113 |
| 77 | +``` |
| 78 | + |
| 79 | +Test access to the host HTTP server from a container: |
| 80 | + |
| 81 | +```console |
| 82 | +% container run -it --rm alpine/curl curl http://host.container.internal:8000 |
| 83 | +hello |
| 84 | +``` |
| 85 | + |
| 86 | +This uses the same underlying DNS mechanism described in [Networking: Set up DNS-based |
| 87 | +container names](./networking.md#set-up-dns-based-container-names), just with |
| 88 | +`--localhost` pointing the domain at a host address instead of a container. |
0 commit comments