Nomad extra_hosts is not propagated into the docker containers /etc/hosts

When you’re using Hashicorp Nomad + Consul Connect the docker extra_hosts argument is not propagated to containers.
Here is a quick workaround to append custom values to /etc/hosts of your containers.
Add the following task in your group, task will start first and exit (0).

    task "extra-etc-hosts" {
      driver = "docker"

      config {
        image   = "alpine:latest"
        command = "/bin/sh"
        args    = ["local/extra_hosts.sh"]
        security_opt   = ["label=disable"]
      }

      template {
        data        = <<EOF
cat <<EOT >> /etc/hosts
10.10.10.10 my.custom-domain.tld
EOT
EOF
        destination = "local/extra_hosts.sh"
      }

      lifecycle {
        hook = "prestart"
      }
    }

Reference of the bug that will soon be fixed, I hope https://github.com/hashicorp/nomad/issues/11056


Posted

in

by

Tags:

Comments

One response to “Nomad extra_hosts is not propagated into the docker containers /etc/hosts”

  1. mehdi Avatar
    mehdi

    Nice, you could also add extra host from sidecar, see this comment on github

    https://github.com/hashicorp/nomad/issues/11056#issuecomment-904152768

Leave a Reply

Your email address will not be published. Required fields are marked *