简体   繁体   English

如何在jenkins管道中编辑Docker镜像中的/ etc / host

[英]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. 我有一个jenkis管道,它创建一个Azure容器实例,其中一些图像被推送(在同一管道上)到Azure注册表。 Now I need a docker container image to be pushed on registry but with a modification on the /etc/hosts file. 现在我需要在注册表上推送一个docker容器映像,但需要对/ etc / hosts文件进行修改。 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: 在pipleine我得到错误:

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 --add主机

" 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): “你可以实现这一点。我不知道如何在jenkins文件中执行此操作。我尝试使用以下jenkins文件(仅添加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? 如何将其添加到/ etc / hosts文件中? 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 在docker image构建阶段您将不具有IP和域名您只能在从docker镜像创建容器时获取此信息

So, my suggestion, would be to use Azure Solution Templates which allows you to invoke scripts after the image is created 因此,我的建议是使用Azure解决方案模板,它允许您在创建映像后调用脚本

Here is one example which does modifies the /etc/hosts post the provision of VM based on docker image 这是一个根据docker镜像修改/etc/hosts后提供VM的示例

https://github.com/Azure/azure-quickstart-templates/blob/f605b4b13a6d372d9f40142323e128b5e5e58440/slurm/azuredeploy.sh 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 您可以在此处找到更多Azure解决方案模板的示例https://github.com/Azure/azure-quickstart-templates

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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