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

docker build --add-host is not working #34078

Closed
arehmandev opened this issue Jul 12, 2017 · 6 comments
Closed

docker build --add-host is not working #34078

arehmandev opened this issue Jul 12, 2017 · 6 comments

Comments

@arehmandev
Copy link

arehmandev commented Jul 12, 2017

I have just recently run:

docker build --add-host=docker:10.180.0.1 -t abs .
docker run -it abs sh
cat /etc/hosts

The new entry is not found in the hosts file - I have tried this on various images (alpine, jboss/wildfly). This was performed on Docker for Mac.

Interestingly this works though:
docker run -it --add-host=docker:10.180.0.1 abs sh
cat /etc/hosts

@thaJeztah
Copy link
Member

I just replied on the other issue #30383 (comment) ;

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

This is working as designed, so i'll close this, but feel free to continue the discussion

@jpuck
Copy link

jpuck commented Jan 19, 2018

@thaJeztah thanks for the answer! I think your one sentence should be added to the documentation for clarification.

What I was looking for was the --add-host flag for the run command.

docker run --add-host=docker:10.180.0.1 -it abs sh
cat /etc/hosts

@thaJeztah
Copy link
Member

@jpuck could you elaborate? it's documented in the docker run command line reference; https://docs.docker.com/edge/engine/reference/commandline/run/#add-entries-to-container-hosts-file-add-host

@thaJeztah
Copy link
Member

oh! you meant my sentence: perhaps you're interested in contributing, and opening a pull request? the markdown file used to generate that page in the documentation can be found here; https://github.com/docker/cli/blob/master/docs/reference/commandline/build.md

@Hokwang
Copy link

Hokwang commented Dec 4, 2018

@thaJeztah is this still true ? can you answer my question? (https://stackoverflow.com/questions/53555902/question-for-docker-build-add-host-command)

and is there a no way to add hosts for users who use images I created ?

what is https://stackoverflow.com/a/46241945/6128380 , I am really confused.

@thaJeztah
Copy link
Member

@Hokwang the same is still true #34078 (comment) / #30383 (comment)

It's by design; the --add-host feature during build is designed to allow overriding a host during build, but not to persist that configuration in the image.

If it would persist in the image;

  • the image would not be portable (i.e., only work in your specific environment)
  • images would be able to spoof DNS (what if an image contained google.com 123.123.123.123 ?)

The person running an image should remain in control over overriding hosts, not the image author; it's a runtime configuration.

Possible solutions For your situation;

  • Run an internal DNS; you can set the default DNS server to use in the daemon; that way every container started will automatically use the configured DNS by default
  • Use docker compose and provide a docker-compose.yml to your developers. The docker compose file allows you to specify all the options that should be used when starting a container, so developers could just docker compose up to start the container with all the options they need to set.

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

No branches or pull requests

5 participants