Skip to content

Please add --add-host=[], --net options to docker build #10324

Closed
@RoelVdP

Description

@RoelVdP

This is a feature request to add the --add-host=[] (currently availble for docker run) to docker build also. This would be very handy.

Activity

resouer

resouer commented on Jan 30, 2015

@resouer
Contributor

@RoelVdP Any reason to why you try to do this ?

RoelVdP

RoelVdP commented on Jan 30, 2015

@RoelVdP
Author

@resouer Yes. In our somewhat complex network setup, we need to edit the host file. We found a workaround here; http://jasonincode.com/customizing-hosts-file-in-docker/ but surely there must be a better way. --add-host=[] would fix this. It also looks like (see other tickets) many people are looking for this. (Hence --add-host=[] was added to docker run, here it is available today.). Having the same for docker build would be fantastic, thank you

cpuguy83

cpuguy83 commented on Jan 30, 2015

@cpuguy83
Member

In general I'm +1 with adding support for things like resource management for the builds, however add-host is specifically something that could make a build work on one host and not on another, and you can't tell why by just looking at the source (Dockerfile).

If you need resolution to these hosts, I'd suggest setting up a DNS server to handle these requests transparently.

So basically, I would have to -1 this one for now, I think.

thaJeztah

thaJeztah commented on Jan 31, 2015

@thaJeztah
Member

however add-host is specifically something that could make a build work on one host and not on another, and you can't tell why by just looking at the source (Dockerfile).

@cpuguy83 playing devils advocate here; having a special DNS on one host and not on another, would also break the build on one host; looking at the Dockerfile also wouldn't explain why it breaks on that host but succeeds on another?

RoelVdP

RoelVdP commented on Feb 2, 2015

@RoelVdP
Author

A DNS server needs to be installed to cover the (fairly basic) functionality of adding a single host IP/name to the hosts file?

cpuguy83

cpuguy83 commented on Feb 2, 2015

@cpuguy83
Member

@RoelVdP My point is, if you want to modify /etc/hosts, do it as part of the build at the beginning of your RUN command.

If the fact that the build isn't working due to needing hosts is an exception to the rule, then yes, fire up a DNS server like github.com/tianon/rawdns to handle this scenario transparently to the build.

@thaJeztah I get your point. And having --add-host would probably be somewhat equivalent of firing up a DNS server... but a feature once added is abused... which makes me (personally) less comfortable with this one in particular.

RoelVdP

RoelVdP commented on Feb 3, 2015

@RoelVdP
Author

@cpuguy83 It is not possible to do it using RUN command/as part of the build; Docker makes this file R/O....

(except when using the complex solution mentioned in the url above http://jasonincode.com/customizing-hosts-file-in-docker/)

It would be great to have this functionality, just like it is available in Docker run.

cpuguy83

cpuguy83 commented on Feb 3, 2015

@cpuguy83
Member

@RoelVdP The build container is the same thing as a docker run container.
/etc/hosts is mounted as read-write, and I checked just to make sure.

FROM busybox
RUN echo 8.8.8.8 foo > /etc/hosts; ping -c 4 foo
RoelVdP

RoelVdP commented on Feb 3, 2015

@RoelVdP
Author

@cpuguy83 A nice one-line workaround you found there, but it's not static;

$ cat Dockerfile
FROM busybox
RUN echo 8.8.8.8 foo > /etc/hosts; cat /etc/hosts
RUN cat /etc/hosts

$ sudo docker build .
Sending build context to Docker daemon 2.048 kB
Sending build context to Docker daemon
Step 0 : FROM busybox
---> 4986bf8c1536
Step 1 : RUN echo 8.8.8.8 foo > /etc/hosts; cat /etc/hosts
---> Running in 22d95c996843
8.8.8.8 foo
---> 5df6fa4f3b7d
Removing intermediate container 22d95c996843
Step 2 : RUN cat /etc/hosts
---> Running in d989b8e95b85
172.17.0.33 7f674915980d
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
---> 1232cf428f6b
Removing intermediate container d989b8e95b85
Successfully built 1232cf428f6b

cpuguy83

cpuguy83 commented on Feb 3, 2015

@cpuguy83
Member

Yep, you'd need to make your changes for each RUN command.

RoelVdP

RoelVdP commented on Feb 3, 2015

@RoelVdP
Author

For anyone who's reading this; thanks to @cpuguy83 we now have the following workaround;

RUN echo ${IP} ${NAME} >> /etc/hosts; ${COMMAND}

The command HAS to be on the same line as the hosts addition (it will not be there the next RUN command), but at least this can be used for each command where needed. Definitely easier then the workaround at http://jasonincode.com/customizing-hosts-file-in-docker/

@cpuguy83 Thanks. While a nice workaround, it would still be great to have the --add-host solution mentioned earlier?

added
kind/featureFunctionality or other elements that the project doesn't currently have. Features are new and shiny
on Feb 26, 2015

104 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/builderarea/networkingkind/featureFunctionality or other elements that the project doesn't currently have. Features are new and shinystatus/needs-attentionCalls for a collective discussion during a review session

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @Bill@vdemeester@fruwe@sebasjm@dieswaytoofast

      Issue actions

        Please add --add-host=[], --net options to docker build · Issue #10324 · moby/moby