简体   繁体   中英

How to edit /etc/host in Docker image from jenkins pipeline

I have problem and not sure how to solve this. I have a jenkis pipeline which creates a Azure container instance with some images that are pushed (on same pipeline) to a Azure registry. Now I need a docker container image to be pushed on registry but with a modification on the /etc/hosts file. I tried with:

RUN  sed -i '$ a 172.x.x.x  host.domain.com ' /etc/hosts

and

RUN echo >> 172.x.x.x  host.domain.com /etc/hosts

And in pipleine I get error:

1msed: can't move '/etc/hostsJCkFPo' to '/etc/hosts': Resource busy

I red that I can´t do such thing during build phase manually, so I search and I looked into, that using "

--add-host

" you can achieve this. Thing is that I don´t know how can I do this in jenkins file. I tried with following inside jenkins file (adding just part of docker build):

 def imageNametom = 'repo.azurecr.io/container-tomcat'
 def imageNameTagged = imageNametom + ':' + env.BUILD_NUMBER

 docker.build("${imageNameTagged}" , "--add-host=host.domain.com:172.x.x.x").

but I get following error:

docker build -t repo.azurecr.io/container-tomcat:6 --add-host=host.domain.com:172.x.x.x
invalid argument "host.domain.com" for "--add-host" flag: bad format for add-host: "host.domain.com"

How Can I add this into /etc/hosts file? so containers created from that image they all have this hosts file.?

thank you

You will not have the IP and Domain name at docker image building phase You only get this information when a container is created from the docker image

So, my suggestion, would be to use Azure Solution Templates which allows you to invoke scripts after the image is created

Here is one example which does modifies the /etc/hosts post the provision of VM based on docker image

https://github.com/Azure/azure-quickstart-templates/blob/f605b4b13a6d372d9f40142323e128b5e5e58440/slurm/azuredeploy.sh

You can find more examples of Azure Solution templates here https://github.com/Azure/azure-quickstart-templates

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM