如何检查容器的 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

查看:24
本文介绍了如何检查容器的 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 "
" | 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,获取它并将其添加到具有本地域名解析的/etc/hosts 文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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