How to inspect a container's IP, obtain it and add it to /etc/hosts file with a local domain name resolution

元气小坏坏 提交于 2019-12-02 18:07:38

问题


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 hostname_assigned 172.20.1.2 docker.dev.example.com

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

This is what i unsuccessfully tried:

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

Command to get container's IP works fine. I put it here as a reference only:

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

回答1:


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

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


来源:https://stackoverflow.com/questions/53877683/how-to-inspect-a-containers-ip-obtain-it-and-add-it-to-etc-hosts-file-with-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!