|
3 | 3 | Learn how `container` networks containers with one another, with the host, and with |
4 | 4 | external systems. |
5 | 5 |
|
6 | | -## Overview |
7 | | - |
8 | 6 | Running `container system start` creates a vmnet network named `default`, to which your |
9 | | -containers attach unless you specify otherwise. Every container on a network receives a |
10 | | -DNS name reachable from the host and from other containers on the same network. The |
11 | | -domain suffix comes from the `[dns] domain` setting in `~/.config/container/config.toml` |
12 | | -(see [`config.toml` reference](./container-system-config.md#dns)) — for example, with |
13 | | -`domain = "test"` set, a container named `my-web-server` is reachable at |
14 | | -`my-web-server.test`. |
| 7 | +containers attach unless you specify otherwise. Every container gets an IP address on |
| 8 | +its network, always reachable by that IP from the host and from other containers on the |
| 9 | +same network (find it with `container inspect <name>`). |
| 10 | + |
| 11 | +## Set up DNS-based container names |
| 12 | + |
| 13 | +Reaching a container by name instead of IP goes through `container`'s embedded DNS |
| 14 | +service. Set this up in two steps: |
| 15 | + |
| 16 | +### Step 1: Tell the `container` service what domain to use |
| 17 | + |
| 18 | +Edit `~/.config/container/config.toml`: |
| 19 | + |
| 20 | +```toml |
| 21 | +[dns] |
| 22 | +domain = "test" |
| 23 | +``` |
| 24 | + |
| 25 | +Restart the service so it picks up the change: |
| 26 | + |
| 27 | +```bash |
| 28 | +container system stop |
| 29 | +container system start |
| 30 | +``` |
| 31 | + |
| 32 | +From this point on, every container you run gets registered under `<name>.test` inside |
| 33 | +`container`'s DNS service, and every container's own DNS resolver is configured to look |
| 34 | +up `.test` names there too. |
| 35 | + |
| 36 | +### Step 2: Tell macOS to use that domain too |
| 37 | + |
| 38 | +Step 1 only affects the `container` service and the containers it runs — your Mac's own |
| 39 | +DNS resolver still knows nothing about `test`. Point it at `container`'s DNS service: |
| 40 | + |
| 41 | +```bash |
| 42 | +sudo container system dns create test |
| 43 | +``` |
| 44 | + |
| 45 | +Enter your administrator password when prompted. This writes a resolver file to |
| 46 | +`/etc/resolver/` that tells macOS: for any `*.test` query, ask `127.0.0.1` instead of |
| 47 | +your normal DNS server. |
| 48 | + |
| 49 | +Both steps are needed. See [`[dns]` reference](./container-system-config.md#dns) for the |
| 50 | +config-key-level detail. |
| 51 | + |
| 52 | +With both steps done, confirm it end-to-end from your Mac: |
| 53 | + |
| 54 | +```console |
| 55 | +% container run -d --rm --name my-web-server python:alpine python3 -m http.server 8000 |
| 56 | +% curl http://my-web-server.test:8000 |
| 57 | +``` |
15 | 58 |
|
16 | 59 | ## Container-to-container networking |
17 | 60 |
|
18 | | -From one container, use another container's DNS name to reach a service it exposes: |
| 61 | +From one container, use another container's DNS name to reach a service it exposes. |
| 62 | +This requires the DNS setup above ([Set up DNS-based container |
| 63 | +names](#set-up-dns-based-container-names)): |
19 | 64 |
|
20 | 65 | ```bash |
21 | 66 | container run --rm -d --name http-server python:alpine python3 -m http.server |
22 | 67 | container run -it --rm alpine/curl curl -v http://http-server.test:8000 |
23 | 68 | container stop http-server |
24 | 69 | ``` |
25 | 70 |
|
| 71 | +> [!WARNING] |
| 72 | +> This works for containers on the `default` network using a domain-qualified name |
| 73 | +> (`http-server.test`, as above). It does **not** currently work for looking up another |
| 74 | +> container by its *bare* hostname (no domain suffix) on a custom network created with |
| 75 | +> `container network create` — the kind of zero-configuration, Compose-style service |
| 76 | +> discovery some users expect. That gap is tracked upstream as |
| 77 | +> [apple/container#1809](https://github.com/apple/container/issues/1809) (open feature |
| 78 | +> request, not yet implemented) and related broader reports in |
| 79 | +> [apple/container#856](https://github.com/apple/container/issues/856). Until resolved, |
| 80 | +> reach a container on a custom network by its IP address instead (`container inspect |
| 81 | +> <name>` to find it). |
| 82 | +
|
26 | 83 | ## Forward traffic from `localhost` to your container |
27 | 84 |
|
28 | 85 | Use the `--publish` option to forward TCP or UDP traffic from your loopback IP to the container you run. The option value has the form `[host-ip:]host-port:container-port[/protocol]`, where protocol may be `tcp` or `udp`, case insensitive. |
@@ -153,9 +210,9 @@ Run `container network list` to see the networks that exist: |
153 | 210 |
|
154 | 211 | ```console |
155 | 212 | % container network list |
156 | | -NETWORK STATE SUBNET |
157 | | -default running 192.168.64.0/24 |
158 | | -foo running 192.168.65.0/24 |
| 213 | +NETWORK SUBNET |
| 214 | +default 192.168.64.0/24 |
| 215 | +foo 192.168.65.0/24 |
159 | 216 | % |
160 | 217 | ``` |
161 | 218 |
|
|
0 commit comments