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

Add --add-host for docker build #30383

Merged
merged 1 commit into from
Feb 27, 2017

Conversation

tabboud
Copy link
Contributor

@tabboud tabboud commented Jan 23, 2017

Signed-off-by: Tony Abboud tdabboud@hotmail.com

- What I did
Added the --add-host flag to the docker 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

@AkihiroSuda AkihiroSuda added area/builder area/networking kind/feature Functionality or other elements that the project doesn't currently have. Features are new and shiny status/1-design-review impact/changelog and removed status/0-triage kind/feature Functionality or other elements that the project doesn't currently have. Features are new and shiny labels Jan 24, 2017
@LK4D4
Copy link
Contributor

LK4D4 commented Jan 31, 2017

Can't this be achieved with ARG or whatever?
ping @docker/core-engine-maintainers

Copy link
Member

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

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

@duglin
Copy link
Contributor

duglin commented Feb 7, 2017

@LK4D4 can you elaborate? Are you imagining some kind of "cat $ARG >> /etc/hosts" type of thing appearing in the Dockerfile?

@@ -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)")
Copy link
Contributor

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.

Copy link
Contributor Author

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",
Copy link
Contributor

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

Copy link
Contributor Author

@tabboud tabboud Feb 9, 2017

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.

@duglin
Copy link
Contributor

duglin commented Feb 7, 2017

In your testing, does the /etc/hosts file get saved in the resulting image?

@duglin
Copy link
Contributor

duglin commented Feb 7, 2017

as long as the changes are not saved in the resulting image, this SGTM

@tabboud
Copy link
Contributor Author

tabboud commented Feb 9, 2017

The /etc/hosts file is not saved in the resulting image. I actually expressed concern about this in #30096

@thaJeztah
Copy link
Member

We discussed this in the maintainers meeting, and although we still think setting up a DNS for these purposes, we already implemented --network for docker build, so let's go ahead and do the same for --add-host

@vdemeester
Copy link
Member

@TDAbboud needs a rebase 👼

@tabboud
Copy link
Contributor Author

tabboud commented Feb 14, 2017

@vdemeester I just re-based and pushed the branch again. I also added another test for some invalid extra hosts.
There currently is no test for a valid IPv6 address, as I was not sure how exactly to implement this. The default /etc/hosts file already contains entries for localhost i.e. ::1 localhost so adding another entry will not work. I think it would be good to test this case as well though, if anyone has a good idea on how to test it.

Copy link
Member

@vdemeester vdemeester left a 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 🦁

@@ -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.
Copy link
Member

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 👼

Copy link
Contributor Author

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

@tabboud tabboud force-pushed the 30096-add-host-docker-build branch from 0b5b724 to c21c5a6 Compare February 14, 2017 13:15
Copy link
Member

@cpuguy83 cpuguy83 left a 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!

@tabboud tabboud force-pushed the 30096-add-host-docker-build branch from c21c5a6 to ad3cfcf Compare February 20, 2017 20:00
Signed-off-by: Tony Abboud <tdabboud@hotmail.com>
@tabboud tabboud force-pushed the 30096-add-host-docker-build branch from ad3cfcf to 7a962e4 Compare February 20, 2017 22:32
@tabboud
Copy link
Contributor Author

tabboud commented Feb 23, 2017

Just pinging to verify all tests have passed and the branch has been rebased

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, thanks!

@thaJeztah thaJeztah merged commit a64ea37 into moby:master Feb 27, 2017
@GordonTheTurtle GordonTheTurtle added this to the 17.04.0 milestone Feb 27, 2017
@thaJeztah
Copy link
Member

/cc @albers @sdurrheimer for completion scripts

@arehmandev
Copy link

Can we confirm this is working? I have just recently tried:

docker build --add-host=docker:10.180.0.1 -t abs
docker exec -it "containerid" sh
cat /etc/hosts - it doesn't appear there

@thaJeztah
Copy link
Member

the host that's added with this flag is only used during the build; it deliberately should not persist in the image

@arehmandev
Copy link

Is this really as intended? I mean other than at runtime, how can you persist host entries to the image?

  • perhaps the best way to approach this would be something like "HOSTENTRY" in the Dockerfile

@cpuguy83
Copy link
Member

@arehmandev You don't. These are generally host specific settings and are not persisted into the image.

@arehmandev
Copy link

So I guess its up to the container orchestration system now to plug in these values. Cool thanks for clarification

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants