Add host entries in docker for mac
Add hosts to docker xhyve VM
Scenario
Previously I have used docker-machine with virtaulbox and recently installed docker for mac. New docker for mac using xhyve VM. In docker-machine we can configure virtualbox to reuse nameservers and host file entries of host machine. We couldn’t use that approach with new docker for mac. When adding host entry with new docker, we need to ssh to docker vm and add host entries to it. Following is the way I have automated these steps.
Connects to docker xhyve VM
There are two ways to do that
1. Unix screen
screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty
2. Run debian docker images
docker run -it --privileged --pid=host debian nsenter -t 1 -m -u -n -i sh
Edit /etc/hosts file
echo -e '10.4.1.4\tdockerregistry.pagero.local' >> /etc/hosts
I’m echoing the host entry to /etc/hosts
file. Instead you can directly edit the file and add the entry.
Exit screen
ctrl + a + d
or use
ctrl-A ctrl-\ and then y (for yes) to exit
Now docker can resolve my host entry dockerregistry.pagero.local
. This host entry exists until restart the docker. If docker restarts it will be lost. Every time restarting the docker(computer) we need to do above steps to add the host entry.
Automate with shell script
I have created shell script which automated above steps. You can add bash alias to this script and simply run it when you need.