如何检查容器的IP,获取容器的IP并将其添加到具有本地域名解析的/etc/hosts文件中 [英] How to inspect a container's IP, obtain it and add it to /etc/hosts file with a local domain name resolution

查看:361
本文介绍了如何检查容器的IP,获取容器的IP并将其添加到具有本地域名解析的/etc/hosts文件中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用docker容器在Linux Ubuntu上工作,我希望将以下内容添加到新行中的/etc/hosts的最后一行:

I am working on Linux Ubuntu with docker containers and I want the following added to the last line of /etc/hosts in a new line:

IP_from_docker_container主机名已分配 172.20.1.2 docker.dev.example.com

IP_from_docker_container hostname_assigned 172.20.1.2 docker.dev.example.com

我一直在尝试以下命令: echo,printf,aux,sed,tee

I have been trying commands such as: echo, printf, aux, sed, tee

这是我尝试失败的方法:

echo "\n" | docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER | echo "       docker.dev.whip-around.com" | sudo tee -a /etc/hosts

获取容器IP的命令正常工作.我在这里仅供参考:

 docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONTAINER_NAME

推荐答案

>>会附加到文件中,因此您可以尝试以下操作:

>> appends to a file, so you can try this:

echo `docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' CONT_NAME` example.com >> /etc/hosts

这篇关于如何检查容器的IP,获取容器的IP并将其添加到具有本地域名解析的/etc/hosts文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆