Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set hosts in docker for mac

Tags:

docker

macos

When I use docker before, I can use docker-machine ssh default to set hosts in docker's machine /etc/hosts, but in docker for mac I can't access it's VM because of it don't have it. So, the problem is how to set hosts in docker for mac ? My secondary domain wants to point the other ip.

like image 895
Cui Avatar asked Jul 23 '16 17:07

Cui


3 Answers

I found a solution, use this command

screen ~/Library/Containers/com.docker.docker/Data/com.docker.driver.amd64-linux/tty

Now, edit the /etc/hosts in the Docker VM.
To exit screen, use Ctrl + a + d.

like image 90
Cui Avatar answered Oct 29 '22 05:10

Cui


Here's how I do it with a bash script so the changes persist between Docker for Mac restarts.

cd ~/Library/Containers/com.docker.docker/Data/database
git reset --hard

DFM_HOSTS_FILE="com.docker.driver.amd64-linux/etc/hosts"
if [ ! -f ${DFM_HOSTS_FILE} ]; then
  echo "appending host to DFM /etc/hosts"
  echo -e "xxx.xxx.xxx.xxx\tmy.special.host" > ${DFM_HOSTS_FILE}
  git add ${DFM_HOSTS_FILE}
  git commit -m "add host to /etc/hosts for dns lookup"
fi
like image 40
Everett Toews Avatar answered Oct 29 '22 03:10

Everett Toews


You can automate it via this script, run this scrip on start up time or login time will save you..

#!/bin/sh 
# host entry -> '10.4.1.4   dockerreigstry.senz.local'
# 1. run debian image
# 2. check host entry exists in /etc/hosts file
# 3. if not exists add it to /etc/hosts file
docker run --name debian -it --privileged --pid=host debian nsenter \
    -t 1 -m -u -n -i sh \
    -c "if ! grep -q dockerregistry.senz.local /etc/hosts; then echo -e '10.4.1.4\tdockerregistry.pagero.local' >> /etc/hosts; fi"

# sleep 2 seconds
# remove stopped debian container
sleep 2
docker rm -f debian

I have created a blog post with more information about this topic.

https://medium.com/@itseranga/set-hosts-in-docker-for-mac-2029276fd448

like image 34
eranga Avatar answered Oct 29 '22 03:10

eranga