In some cases you may need to send data from your Docker containers to your local machine. This could be for sending data to a local webserver or something else entirely, but as it isn’t something that Docker directly supports, we need a bit of a workaraound to achieve it. Luckily, this is why this gist exists. No need to scour the web for hours and contemplate your existance in search for a solution, i’ve already done that part for you!
So there are two ways of achieving this, depending on if you’re running Ubuntu or macOS, as Docker runs inside of a VM on macOS.
-
On Ubuntu:
-
Add the following alias to your
.bashrc
(or whatever shell you’re using):alias hostip="ip route show 0.0.0.0/0 | grep -Eo 'via \S+' | awk '{ print \$2 }'"
-
Next you need to add the following to your
docker-compose.yml
on all the containers needing access:extra_hosts: - "LOCAL_DOMAIN:$(hostip)"
Where
LOCAL_DOMAIN
is the domain you need it to access.
-
-
On macOS:
-
Add the following alias to your
.bashrc
(or whatever shell you’re using):alias hostip="sudo ifconfig lo0 alias 172.16.123.1 && echo '172.16.123.1'"
-
Next you need to add the following to your
docker-compose.yml
on all the containers needing access:extra_hosts: - "LOCAL_DOMAIN:$(hostip)"
Where
LOCAL_DOMAIN
is the domain you need it to access.
-