This adds a --add-host host:ip flag which appends lines to /etc/hosts. This is needed in places where you want the container to get a different name resolution than it would through DNS. This was submitted before as #5525, closed, and now I am re-opening. It has come up 2 or 3 times in the last couple days.
Signed-off-by: Tim Hockin <thockin@google.com>
| ... | ... |
@@ -422,6 +422,11 @@ func (container *Container) buildHostsFiles(IP string) error {
|
| 422 | 422 |
extraContent[alias] = child.NetworkSettings.IPAddress |
| 423 | 423 |
} |
| 424 | 424 |
|
| 425 |
+ for _, extraHost := range container.hostConfig.ExtraHosts {
|
|
| 426 |
+ parts := strings.Split(extraHost, ":") |
|
| 427 |
+ extraContent[parts[0]] = parts[1] |
|
| 428 |
+ } |
|
| 429 |
+ |
|
| 425 | 430 |
return etchosts.Build(container.HostsPath, IP, container.Config.Hostname, container.Config.Domainname, &extraContent) |
| 426 | 431 |
} |
| 427 | 432 |
|
| ... | ... |
@@ -7,6 +7,7 @@ docker-run - Run a command in a new container |
| 7 | 7 |
# SYNOPSIS |
| 8 | 8 |
**docker run** |
| 9 | 9 |
[**-a**|**--attach**[=*[]*]] |
| 10 |
+[**--add-host**[=*[]*]] |
|
| 10 | 11 |
[**-c**|**--cpu-shares**[=*0*]] |
| 11 | 12 |
[**--cap-add**[=*[]*]] |
| 12 | 13 |
[**--cap-drop**[=*[]*]] |
| ... | ... |
@@ -64,6 +65,10 @@ error. It can even pretend to be a TTY (this is what most commandline |
| 64 | 64 |
executables expect) and pass along signals. The **-a** option can be set for |
| 65 | 65 |
each of stdin, stdout, and stderr. |
| 66 | 66 |
|
| 67 |
+**--add-host**=*hostname*:*ip* |
|
| 68 |
+ Add a line to /etc/hosts. The format is hostname:ip. The **--add-host** |
|
| 69 |
+option can be set multiple times. |
|
| 70 |
+ |
|
| 67 | 71 |
**-c**, **--cpu-shares**=0 |
| 68 | 72 |
CPU shares in relative weight. You can increase the priority of a container |
| 69 | 73 |
with the -c option. By default, all containers run at the same priority and get |
| ... | ... |
@@ -986,6 +986,7 @@ removed before the image is removed. |
| 986 | 986 |
Run a command in a new container |
| 987 | 987 |
|
| 988 | 988 |
-a, --attach=[] Attach to STDIN, STDOUT or STDERR. |
| 989 |
+ --add-host=[] Add a custom host-to-IP mapping (host:ip) |
|
| 989 | 990 |
-c, --cpu-shares=0 CPU shares (relative weight) |
| 990 | 991 |
--cap-add=[] Add Linux capabilities |
| 991 | 992 |
--cap-drop=[] Drop Linux capabilities |
| ... | ... |
@@ -139,6 +139,7 @@ example, `docker run ubuntu:14.04`. |
| 139 | 139 |
'none': no networking for this container |
| 140 | 140 |
'container:<name|id>': reuses another container network stack |
| 141 | 141 |
'host': use the host network stack inside the container |
| 142 |
+ --add-host="" : Add a line to /etc/hosts (host:IP) |
|
| 142 | 143 |
|
| 143 | 144 |
By default, all containers have networking enabled and they can make any |
| 144 | 145 |
outgoing connections. The operator can completely disable networking |
| ... | ... |
@@ -196,6 +197,22 @@ running the `redis-cli` command and connecting to the Redis server over the |
| 196 | 196 |
$ # use the redis container's network stack to access localhost |
| 197 | 197 |
$ sudo docker run --rm -ti --net container:redis example/redis-cli -h 127.0.0.1 |
| 198 | 198 |
|
| 199 |
+### Managing /etc/hosts |
|
| 200 |
+ |
|
| 201 |
+Your container will have lines in `/etc/hosts` which define the hostname of the |
|
| 202 |
+container itself as well as `localhost` and a few other common things. The |
|
| 203 |
+`--add-host` flag can be used to add additional lines to `/etc/hosts`. |
|
| 204 |
+ |
|
| 205 |
+ $ /docker run -ti --add-host db-static:86.75.30.9 ubuntu cat /etc/hosts |
|
| 206 |
+ 172.17.0.22 09d03f76bf2c |
|
| 207 |
+ fe00::0 ip6-localnet |
|
| 208 |
+ ff00::0 ip6-mcastprefix |
|
| 209 |
+ ff02::1 ip6-allnodes |
|
| 210 |
+ ff02::2 ip6-allrouters |
|
| 211 |
+ 127.0.0.1 localhost |
|
| 212 |
+ ::1 localhost ip6-localhost ip6-loopback |
|
| 213 |
+ 86.75.30.9 db-static |
|
| 214 |
+ |
|
| 199 | 215 |
## Clean Up (–-rm) |
| 200 | 216 |
|
| 201 | 217 |
By default a container's file system persists even after the container |
| ... | ... |
@@ -1331,6 +1331,23 @@ func TestDnsOptionsBasedOnHostResolvConf(t *testing.T) {
|
| 1331 | 1331 |
logDone("run - dns options based on host resolv.conf")
|
| 1332 | 1332 |
} |
| 1333 | 1333 |
|
| 1334 |
+func TestRunAddHost(t *testing.T) {
|
|
| 1335 |
+ defer deleteAllContainers() |
|
| 1336 |
+ cmd := exec.Command(dockerBinary, "run", "--add-host=extra:86.75.30.9", "busybox", "grep", "extra", "/etc/hosts") |
|
| 1337 |
+ |
|
| 1338 |
+ out, _, err := runCommandWithOutput(cmd) |
|
| 1339 |
+ if err != nil {
|
|
| 1340 |
+ t.Fatal(err, out) |
|
| 1341 |
+ } |
|
| 1342 |
+ |
|
| 1343 |
+ actual := strings.Trim(out, "\r\n") |
|
| 1344 |
+ if actual != "86.75.30.9\textra" {
|
|
| 1345 |
+ t.Fatalf("expected '86.75.30.9\textra', but says: '%s'", actual)
|
|
| 1346 |
+ } |
|
| 1347 |
+ |
|
| 1348 |
+ logDone("run - add-host option")
|
|
| 1349 |
+} |
|
| 1350 |
+ |
|
| 1334 | 1351 |
// Regression test for #6983 |
| 1335 | 1352 |
func TestAttachStdErrOnlyTTYMode(t *testing.T) {
|
| 1336 | 1353 |
cmd := exec.Command(dockerBinary, "run", "-t", "-a", "stderr", "busybox", "true") |
| ... | ... |
@@ -199,6 +199,17 @@ func validateDomain(val string) (string, error) {
|
| 199 | 199 |
return "", fmt.Errorf("%s is not a valid domain", val)
|
| 200 | 200 |
} |
| 201 | 201 |
|
| 202 |
+func ValidateExtraHost(val string) (string, error) {
|
|
| 203 |
+ arr := strings.Split(val, ":") |
|
| 204 |
+ if len(arr) != 2 || len(arr[0]) == 0 {
|
|
| 205 |
+ return "", fmt.Errorf("bad format for add-host: %s", val)
|
|
| 206 |
+ } |
|
| 207 |
+ if _, err := ValidateIPAddress(arr[1]); err != nil {
|
|
| 208 |
+ return "", fmt.Errorf("bad format for add-host: %s", val)
|
|
| 209 |
+ } |
|
| 210 |
+ return val, nil |
|
| 211 |
+} |
|
| 212 |
+ |
|
| 202 | 213 |
// Validates an HTTP(S) registry mirror |
| 203 | 214 |
func ValidateMirror(val string) (string, error) {
|
| 204 | 215 |
uri, err := url.Parse(val) |
| ... | ... |
@@ -49,6 +49,7 @@ type HostConfig struct {
|
| 49 | 49 |
PublishAllPorts bool |
| 50 | 50 |
Dns []string |
| 51 | 51 |
DnsSearch []string |
| 52 |
+ ExtraHosts []string |
|
| 52 | 53 |
VolumesFrom []string |
| 53 | 54 |
Devices []DeviceMapping |
| 54 | 55 |
NetworkMode NetworkMode |
| ... | ... |
@@ -81,6 +82,9 @@ func ContainerHostConfigFromJob(job *engine.Job) *HostConfig {
|
| 81 | 81 |
if DnsSearch := job.GetenvList("DnsSearch"); DnsSearch != nil {
|
| 82 | 82 |
hostConfig.DnsSearch = DnsSearch |
| 83 | 83 |
} |
| 84 |
+ if ExtraHosts := job.GetenvList("ExtraHosts"); ExtraHosts != nil {
|
|
| 85 |
+ hostConfig.ExtraHosts = ExtraHosts |
|
| 86 |
+ } |
|
| 84 | 87 |
if VolumesFrom := job.GetenvList("VolumesFrom"); VolumesFrom != nil {
|
| 85 | 88 |
hostConfig.VolumesFrom = VolumesFrom |
| 86 | 89 |
} |
| ... | ... |
@@ -54,6 +54,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf |
| 54 | 54 |
flExpose = opts.NewListOpts(nil) |
| 55 | 55 |
flDns = opts.NewListOpts(opts.ValidateIPAddress) |
| 56 | 56 |
flDnsSearch = opts.NewListOpts(opts.ValidateDnsSearch) |
| 57 |
+ flExtraHosts = opts.NewListOpts(opts.ValidateExtraHost) |
|
| 57 | 58 |
flVolumesFrom = opts.NewListOpts(nil) |
| 58 | 59 |
flLxcOpts = opts.NewListOpts(nil) |
| 59 | 60 |
flEnvFile = opts.NewListOpts(nil) |
| ... | ... |
@@ -93,6 +94,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf |
| 93 | 93 |
cmd.Var(&flExpose, []string{"#expose", "-expose"}, "Expose a port from the container without publishing it to your host")
|
| 94 | 94 |
cmd.Var(&flDns, []string{"#dns", "-dns"}, "Set custom DNS servers")
|
| 95 | 95 |
cmd.Var(&flDnsSearch, []string{"-dns-search"}, "Set custom DNS search domains")
|
| 96 |
+ cmd.Var(&flExtraHosts, []string{"-add-host"}, "Add a custom host-to-IP mapping (host:ip)")
|
|
| 96 | 97 |
cmd.Var(&flVolumesFrom, []string{"#volumes-from", "-volumes-from"}, "Mount volumes from the specified container(s)")
|
| 97 | 98 |
cmd.Var(&flLxcOpts, []string{"#lxc-conf", "-lxc-conf"}, "(lxc exec-driver only) Add custom lxc options --lxc-conf=\"lxc.cgroup.cpuset.cpus = 0,1\"")
|
| 98 | 99 |
|
| ... | ... |
@@ -291,6 +293,7 @@ func parseRun(cmd *flag.FlagSet, args []string, sysInfo *sysinfo.SysInfo) (*Conf |
| 291 | 291 |
PublishAllPorts: *flPublishAll, |
| 292 | 292 |
Dns: flDns.GetAll(), |
| 293 | 293 |
DnsSearch: flDnsSearch.GetAll(), |
| 294 |
+ ExtraHosts: flExtraHosts.GetAll(), |
|
| 294 | 295 |
VolumesFrom: flVolumesFrom.GetAll(), |
| 295 | 296 |
NetworkMode: netMode, |
| 296 | 297 |
Devices: deviceMappings, |