kaniko icon indicating copy to clipboard operation
kaniko copied to clipboard

/etc/hosts in docker image ignored

Open viceice opened this issue 5 years ago • 8 comments

Actual behavior Kaniko is ignoring /etc/hosts configured by docker

Expected behavior Kaniko should respect /etc/hosts

To Reproduce Steps to reproduce the behavior:

  1. run kaniko docker image with extrahosts for redirecting registry

Additional Information This is caused by missing /etc/nsswitch.conf : echo "hosts: files dns" > /etc/nsswitch.conf`` See https://github.com/golang/go/issues/22846 for details

viceice avatar Mar 13 '19 07:03 viceice

Thanks! Would you like to try fixing this? It should be as simple as modifying the Dockerfiles in https://github.com/GoogleContainerTools/kaniko/blob/master/deploy/Dockerfile

dlorenc avatar Mar 19 '19 14:03 dlorenc

I think adding this file is not enough. When i use a multistage build, /etc/ will get deleted.

We need a Workaround for redirecting our /etc to /kaniko/etc/.

viceice avatar Mar 19 '19 14:03 viceice

Unpacking a image will overwrite that files too.

viceice avatar Mar 19 '19 14:03 viceice

Hmm, if you mount it in as a volume during the run (either docker or k8s) then it should not get overwritten. Would that work for your use case?

dlorenc avatar Mar 19 '19 14:03 dlorenc

that could work, does docker mount the hosts file or do i have to mount that when i run kaniko container?

viceice avatar Mar 19 '19 14:03 viceice

@ViceIce you will have to mount it explicitly

tejal29 avatar Jan 10 '20 23:01 tejal29

may be we can have a '--add-host' option like docker ?

Windforce17 avatar May 11 '21 14:05 Windforce17

I found a workaround that worked for me that I wanted to share:

Background:
Docker does not allow to modify the /etc/host at build time. The solution is to use --extra-hosts to provide domain resulution while building. Kaniko does not support this.

Note This is not a solution just a dirty workaround

I will assume that most of you will run kaniko in CI. I use GitLab

So we can override the entrypoint and run a before_script:

Assuming you habe a domains.list file (however you generate or get it)

Before running kaniko do:

for(line in "$(<domains.list)") {
  echo "127.0.0.1 ${line}" >> /etc/hosts
} 

Now my domains in the reverse proxy config are resolvable and the config test passes.

hegerdes avatar Jun 14 '22 12:06 hegerdes

for(line in "$(<domains.list)") {
  echo "127.0.0.1 ${line}" >> /etc/hosts
} 

is this workaround works? the before_script seems wrong?

CharkeyQK avatar Sep 26 '23 02:09 CharkeyQK