Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

question for docker build --add-host command

Tags:

I am an infra admin for providing docker images to developers.

I created "A" images and then tell docker run command is

docker run --add-host=a-lic:10.0.0.1 --add-host=b-lic:10.0.0.2 A

every developers request to me, please remove --add-host option because it is long. So I want to edit /etc/hosts file when docker build if possible.

I find out docker build --add-host option newly create from 17.04 but it does not work as my expected.

someone said --add-host option is for only during building image and another said --add-host option will work as below (my thoughts).

docker build --add-host=a-lic:10.0.0.1 -t A .
docker run -it A

And docker's documentation is not sufficient for this.

$ docker build --help
Usage:  docker build [OPTIONS] PATH | URL | -

Build an image from a Dockerfile

Options:
      --add-host list           Add a custom host-to-IP mapping (host:ip)

What is the correct??

like image 400
hokwang Avatar asked Nov 30 '18 10:11

hokwang


1 Answers

It's by design (see https://github.com/moby/moby/issues/34078#issuecomment-314798584 / https://github.com/moby/moby/pull/30383#issuecomment-314797629); 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.
like image 77
thaJeztah Avatar answered Nov 09 '22 20:11

thaJeztah