Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Permit '=' separator and '[ipv6]' in --add-host #2121

Merged
merged 1 commit into from
Nov 23, 2023

Conversation

robmry
Copy link
Contributor

@robmry robmry commented Nov 16, 2023

- What I did

Make it easier to specify IPv6 addresses in the '--add-host' option by permitting 'host=ip' in addition to 'host:ip', and allowing square brackets around the address.

For example:

--add-host=hostname:127.0.0.1
--add-host=hostname:::1
--add-host=hostname=::1
--add-host=hostname:[::1]

- How I did it

Updated the function that converts from --add-host format to buildx's csv format, stripping brackets from the address.

This pull request is equivalent to one in docker/cli, for docker run and legacy builds.

- How to verify it

New unit tests pass. And ...

/work # cat Dockerfile
FROM alpine:latest
RUN ping -c1 something
RUN ping -c1 somethingv6

/work # docker build --add-host something=8.8.8.8 --add-host somethingv6=[::1] . 2>&1 | cat
#0 building with "default" instance using docker driver

#1 [internal] load .dockerignore
#1 transferring context: 2B done
#1 DONE 0.0s

#2 [internal] load build definition from Dockerfile
#2 transferring dockerfile: 104B done
#2 DONE 0.0s

#3 [internal] load metadata for docker.io/library/alpine:latest
#3 DONE 0.9s

#4 [1/3] FROM docker.io/library/alpine:latest@sha256:eece025e432126ce23f223450a0326fbebde39cdf496a85d8c016293fc851978
#4 CACHED

#5 [2/3] RUN ping -c1 something
#5 0.116 PING something (8.8.8.8): 56 data bytes
#5 0.190 64 bytes from 8.8.8.8: seq=0 ttl=62 time=73.792 ms
#5 0.190
#5 0.190 --- something ping statistics ---
#5 0.190 1 packets transmitted, 1 packets received, 0% packet loss
#5 0.190 round-trip min/avg/max = 73.792/73.792/73.792 ms
#5 DONE 0.2s

#6 [3/3] RUN ping -c1 somethingv6
#6 0.205 PING somethingv6 (::1): 56 data bytes
#6 0.205 64 bytes from ::1: seq=0 ttl=64 time=0.028 ms
#6 0.205
#6 0.205 --- somethingv6 ping statistics ---
#6 0.205 1 packets transmitted, 1 packets received, 0% packet loss
#6 0.205 round-trip min/avg/max = 0.028/0.028/0.028 ms
#6 DONE 0.2s

#7 exporting to image
#7 exporting layers done
#7 writing image sha256:ea192cc9965b812a66562225e85b437af3af1d8347daca9d65eff8a6498b679e done
#7 DONE 0.0s

- Description for the changelog

Permit '=' separator and '[ipv6]' in --add-host

Comment on lines +68 to +71
host, ip, ok := strings.Cut(h, "=")
if !ok {
host, ip, ok = strings.Cut(h, ":")
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we update the reference docs in

flags.StringSliceVar(&options.extraHosts, "add-host", []string{}, `Add a custom host-to-IP mapping (format: "host:ip")`)
?

Guess it should be ... (format: "host=ip") if this format is the preferred one now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, yes - will do! Thank you.

Copy link
Member

@crazy-max crazy-max left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@robmry
Copy link
Contributor Author

robmry commented Nov 21, 2023

The changes for CLI and buildx are done - but docker-compose has an "extra_hosts" option that'll need the same treatment.

@robmry
Copy link
Contributor Author

robmry commented Nov 21, 2023

Updated to use domain ".invalid" in unit tests - as per @thaJeztah's comment on the CLI review, docker/cli#4663 (comment)

Copy link
Member

@thaJeztah thaJeztah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@thaJeztah
Copy link
Member

I added a cherry-pick label for consideration (so that buildx and cli will align on accepted formats)

@@ -15,7 +15,7 @@ Start a build

| Name | Type | Default | Description |
|:-------------------------------------------------------------------------------------------------------------------------------------------------------|:--------------|:----------|:----------------------------------------------------------------------------------------------------|
| [`--add-host`](https://docs.docker.com/engine/reference/commandline/build/#add-host) | `stringSlice` | | Add a custom host-to-IP mapping (format: `host:ip`) |
| [`--add-host`](https://docs.docker.com/engine/reference/commandline/build/#add-host) | `stringSlice` | | Add a custom host-to-IP mapping (format: `host=ip`) |
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we should just switch the formatting in docs as it takes time for people to upgrade to the latest versions. Doesn't look like the old formatting is deprecated as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I see what you mean - the online docs aren't very version-specific.

This part of the ".md" is generated from the flags.StringSliceVar() text. We could put both in there with an "or", but that may not really help to make it obvious that the : is for backward compatibility - and we probably don't want an essay in there. Earlier comments have been about preferring ("soft-deprecating") the colon separator.

On the other hand, the "--add-host" in the first column here is a link to the cli/build docs, here it is in my branch ...

https://github.com/robmry/buildx/blob/align_add-host_with_cli/docs/reference/buildx_build.md

And the modified version of the page it'll link to is here ...

https://github.com/robmry/cli/blob/4648-clearer_ipv6_in_add-host/docs/reference/commandline/build.md#add-host

It says ...

You can wrap an IPv6 address in square brackets. = and : are both valid separators. Both formats in the following example are valid:

... perhaps we should make it clear there that the : is for backward compatibility?

@dvdksn - any suggestions?

@@ -486,7 +486,7 @@ func buildCmd(dockerCli command.Cli, rootOpts *rootOptions, debugConfig *debug.D

flags := cmd.Flags()

flags.StringSliceVar(&options.extraHosts, "add-host", []string{}, `Add a custom host-to-IP mapping (format: "host:ip")`)
flags.StringSliceVar(&options.extraHosts, "add-host", []string{}, `Add a custom host-to-IP mapping (format: "host=ip")`)
Copy link
Contributor

@dvdksn dvdksn Nov 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could keep the example as : here, until we officially deprecate that separator. That would give users time to upgrade to later versions which would support both syntaxes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, will do ...

@crazy-max - this will undo the earlier change for your #2121 (comment) - but I think it makes sense given #2121 (comment) ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes agree let's keep : for now

Fixes docker/cli#4648

Make it easier to specify IPv6 addresses in the '--add-host' option by
permitting 'host=ip' in addition to 'host:ip', and allowing square
brackets around the address.

For example:

    --add-host=hostname:127.0.0.1
    --add-host=hostname:::1
    --add-host=hostname=::1
    --add-host=hostname=[::1]

Signed-off-by: Rob Murray <rob.murray@docker.com>
@crazy-max crazy-max merged commit 9368ecb into docker:master Nov 23, 2023
61 checks passed
@crazy-max crazy-max added this to the v0.13.0 milestone Dec 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ipv6 addresses with square brackets cannot be used with the --add-host option
6 participants