Skip to content

Instantly share code, notes, and snippets.

@andrius
Last active March 26, 2018 19:37
Show Gist options
  • Save andrius/e81b04a83571bcb9f421066733f8ce30 to your computer and use it in GitHub Desktop.
Save andrius/e81b04a83571bcb9f421066733f8ce30 to your computer and use it in GitHub Desktop.
IP address of docker host (mac) in shell scripts. Only works with #docker for #macos
#!/bin/sh
# Tested on alpine linux
# Thanks to `docker.for.mac.localhost` (or `docker.for.mac.host.internal`)
# featured in https://docs.docker.com/docker-for-mac/networking/#i-want-to-connect-from-a-container-to-a-service-on-the-host,
# it's possible to access host (mac) services
# mac_ip will set IP address (MAC_IP) of mac visible from docker container and
# return code (0 = IP found, 1 = not found)
mac_ip() {
MAC_IP=`ping -c 1 docker.for.mac.host.internal 2>/dev/null | grep ' from ' | awk -F ' \|:' '{print $4}'`
[ "${MAC_IP}" != "" ] && return 0 || return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment