-
Notifications
You must be signed in to change notification settings - Fork 18.7k
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
Add --add-host for docker build #30383
Conversation
Can't this be achieved with ARG or whatever? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LK4D4 can you elaborate? Are you imagining some kind of "cat $ARG >> /etc/hosts" type of thing appearing in the Dockerfile? |
cli/command/image/build.go
Outdated
@@ -107,6 +109,7 @@ func NewBuildCommand(dockerCli *command.DockerCli) *cobra.Command { | |||
flags.BoolVar(&options.compress, "compress", false, "Compress the build context using gzip") | |||
flags.StringSliceVar(&options.securityOpt, "security-opt", []string{}, "Security options") | |||
flags.StringVar(&options.networkMode, "network", "default", "Set the networking mode for the RUN instructions during build") | |||
flags.Var(&options.extraHosts, "add-host", "Add a custom host-to-IP mapping (host:ip)") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thank you for adding the "form" to the help text - I can never remember that stuff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 me neither
buildImageSuccessfully(c, name, | ||
withBuildFlags( | ||
"--add-host", "foo:127.0.0.1", | ||
"--add-host", "bar:127.0.0.1", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know we're in the design phase and this is premature, but if I don't ask now I'll probably forget....
can you add some negative tests? For example --add-host myhost:
or --add-host myhost
and ones with bad IPs. And ones with ipv6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes total sense. I was thinking about this when I wrote the initial test. I will add some more to this PR and push it again.
In your testing, does the /etc/hosts file get saved in the resulting image? |
as long as the changes are not saved in the resulting image, this SGTM |
The |
We discussed this in the maintainers meeting, and although we still think setting up a DNS for these purposes, we already implemented |
@TDAbboud needs a rebase 👼 |
8b10e11
to
0b5b724
Compare
@vdemeester I just re-based and pushed the branch again. I also added another test for some invalid extra hosts. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small nit on the API changes, but otherwise LGTM 🦁
docs/api/version-history.md
Outdated
@@ -34,6 +34,7 @@ keywords: "API, Docker, rcli, REST, documentation" | |||
* The API version is now required in all API calls. Instead of just requesting, for example, the URL `/containers/json`, you must now request `/v1.25/containers/json`. | |||
* `GET /version` now returns `MinAPIVersion`. | |||
* `POST /build` accepts `networkmode` parameter to specify network used during build. | |||
* `POST /build` now accepts `extrahosts` parameter to specify a host to ip mapping to use during the build. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not at the right place, should be in v1.27 API changes
👼
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I just fixed this and re-pushed
0b5b724
to
c21c5a6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
We are currently having issues with CI, so we need to make sure to wait for CI to run against this PR.
Thanks!
c21c5a6
to
ad3cfcf
Compare
Signed-off-by: Tony Abboud <tdabboud@hotmail.com>
ad3cfcf
to
7a962e4
Compare
Just pinging to verify all tests have passed and the branch has been rebased |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
/cc @albers @sdurrheimer for completion scripts |
Add --add-host for docker build
Add --add-host for docker build
Can we confirm this is working? I have just recently tried: docker build --add-host=docker:10.180.0.1 -t abs |
the host that's added with this flag is only used during the build; it deliberately should not persist in the image |
Is this really as intended? I mean other than at runtime, how can you persist host entries to the image?
|
@arehmandev You don't. These are generally host specific settings and are not persisted into the image. |
So I guess its up to the container orchestration system now to plug in these values. Cool thanks for clarification |
Signed-off-by: Tony Abboud tdabboud@hotmail.com
- What I did
Added the
--add-host
flag to thedocker build
command. This flag allows you to add extra hosts to a containers /etc/hosts file upon building an image.- How I did it
I added another flag to the build handler and added server side code to accept and set the extra hosts when the
/build
endpoint is hit.- How to verify it
I have added an integration test
TestBuildWithExtraHost
which adds hosts whose ip is set to the localhost, but then it tries to ping based off of the hostname that was set.- Description for the changelog
Add the --add-host flag to docker build to add entries to a containers /etc/hosts file when building an image
Implements #30096