register and update docker containers ips in etc hosts

register and update docker containers ips in etc hosts

Use following script to update /etc/hosts with entries of docker containers mycontainersXXXXwitr.local

# delete lines between '# docker-compose containers start' and '# docker-compose containers end' in /etc/hosts
sudo sed -i.bak '/^# docker-compose containers start/,/# docker-compose containers end/d' /etc/hosts


echo -e "\nyour /etc/hosts is updated with following:"
echo "======================"
echo "" | sudo tee -a /etc/hosts
echo "# docker-compose containers start" | sudo tee -a /etc/hosts
echo "" | sudo tee -a /etc/hosts
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}} {{.Name}}' $(docker ps --filter name=mycontainers.*witr.local -q) | sed 's/\///' | sudo tee -a /etc/hosts
echo "" | sudo tee -a /etc/hosts
echo "# docker-compose containers end" | sudo tee -a /etc/hosts
echo "======================"
admin

Leave a Reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four 
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax