Don't drop host aliases from swarm extra hosts - #53338
Conversation
Signed-off-by: Ali Afsharzadeh <[email protected]>
🤔 I actually wonder if that's intentional, or just a missing validation / handling for the spaces; At a quick glance, at least passing multiple hostnames there isn't documented, so if it works, that's very well possible undocumented behavior, and not intentional; https://docs.docker.com/reference/cli/docker/container/run/#add-host
|
I went through the history a bit. You are right that the multi name form is not documented anywhere, the docs and the API reference only show a single hostname, and no test covers it. But it does not look like an accident to me either. The original Since the behavior has been there and stable for that long, I would rather make it official than treat it as an accident. I can document it (the |
|
Is adding multiple docker service create --name myservice --host host1:1.1.1.1 --host host2:1.1.1.1 nginx:alpine
docker ps -n1
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
919895b58df9 nginx:alpine "/docker-entrypoint.…" About a minute ago Up About a minute 80/tcp myservice.1.tl0ocyp0vkpln9wzr8qi0cp9p
docker exec 919895b58df9 cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00:: ip6-localnet
ff00:: ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
1.1.1.1 host1
1.1.1.1 host2
172.17.0.2 919895b58df9 |
|
The docker run --rm \
--add-host "host1 host2=1.1.1.1" \
--add-host "host2=1.1.1.1" \
--add-host "host2 host1=1.1.1.1" \
--add-host "host2=2.2.2.2" \
--add-host "host1 # I can haz comments=2.2.2.2" \
--add-host "host3,host4,host5=2.2.2.2" \
--add-host "$(printf 'host6\n# comment line\nhost7 host8 1.1.1.1=2.2.2.2')" \
alpine cat /etc/hosts127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00:: ip6-localnet
ff00:: ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
1.1.1.1 host1 host2
1.1.1.1 host2
1.1.1.1 host2 host1
2.2.2.2 host2
2.2.2.2 host1 # I can haz comments
2.2.2.2 host3,host4,host5
2.2.2.2 host6
# comment line
host7 host8 1.1.1.1
172.17.0.3 848ddcee0411We should fix that, because it's clearly not intentional; for swarm services, this would likely also be problematic for If this is an important use-case, we should probably look at an alternative syntax, similar to what we did for # short form (legacy)
--add-host host1:1.1.1.1
# short form
--add-host host1=1.1.1.1
# advanced syntax
--add-host name=host1,alias=host2,alias=host3,ip=1.1.1.1And, likely send it in more structured format over the API; something like; type HostEntry struct {
IP netip.Addr
Name string
Aliases []string
} |
For plain name to IP resolution, yes, multiple The two layouts are not the same for the resolver though. On a single line the first name is the canonical hostname and the rest are aliases, on separate lines every name is its own canonical name: $ # one line: "1.1.1.1 host1 host2"
$ docker run --rm --add-host 'host1 host2:1.1.1.1' debian:stable-slim getent hosts host2
1.1.1.1 host1 host2
$ # separate lines: "1.1.1.1 host1" and "1.1.1.1 host2"
$ docker run --rm --add-host host1:1.1.1.1 --add-host host2:1.1.1.1 debian:stable-slim getent hosts host2
1.1.1.1 host2
The main problem this PR wants to fix is not the exact file layout. The |
that part already deals with multi name entries correctly. Removal is per name, not per entry, that was made granular in docker/cli#1054: $ docker service create --name rmtest --host 'host1 host2:1.1.1.1' busybox sleep 600
$ docker service inspect rmtest --format '{{json .Spec.TaskTemplate.ContainerSpec.Hosts}}'
["1.1.1.1 host1 host2"]
$ docker service update --host-rm host2 rmtest
$ docker service inspect rmtest --format '{{json .Spec.TaskTemplate.ContainerSpec.Hosts}}'
["1.1.1.1 host1"] |
Thanks! Yes, I'm aware they're not identical; it was me being curious if your use-case was specifically for this purpose, or if the existing "multiple host entries" would work as a solution (at least for now).
You're right; I was mostly looking at the "container" equivalent, which at the code-level is pretty under-defined; moby/api/types/container/hostconfig.go Line 439 in 689b208 It's slightly better in the API definition; Lines 1160 to 1166 in 689b208 I'll try to have a closer look at your PR later. |
|
For the (follow-up) work on the non-swarm (container) bit; just writing it down here, but probably could use a separate ticket; Perhaps instead of a structured format in the API, we should follow the same approach as Swarm does; the format used in For the container part, besides fixing the CLI-side, I think we should do a transition to a new field;
|
--hosttoservice createand--host-add/rmtoservice update#28031Summary
Swarm stores extra hosts in the hosts(5) format,
IP_address canonical_hostname [aliases...].That format was picked in #28031 on purpose, so that aliases would not get lost.
The executor never implemented that part though. When it converts spec entries
to
HostConfig.ExtraHostsit keeps the first hostname and drops the rest("Alias is ignored for now").
With this change all names are kept, so
"1.1.1.1 host1 host2"becomes"host1 host2:1.1.1.1"and shows up as one line in the container's/etc/hosts:Plain containers already work like this,
docker run --add-host "host1 host2:1.1.1.1"gives the same line. No CLI changes are needed,
service update --host-rmalready knows how to deal with multi-name entries.
To verify:
Before this change the line only contains host1, now it has both names.
Release notes (optional)
A picture of a cute animal (not mandatory but encouraged)