Skip to content

Instantly share code, notes, and snippets.

@subfuzion
Last active October 11, 2020 18:47
Show Gist options
  • Save subfuzion/f9cc30eec6936d420d8a to your computer and use it in GitHub Desktop.
Save subfuzion/f9cc30eec6936d420d8a to your computer and use it in GitHub Desktop.
Get Docker container IP and Port

Given a docker machine (started with docker-machine) and a docker-compose.yml file with a service (ex: web) that exposes a port (ex: 3000), get the address for a running container, ex:

$ addr web 3000
192.168.99.100:32768

If not running from directory containing docker-compose.yml, or if you want to use an alternative .yml file, specify the file pathname with the 3rd parameter, ex:

$ addr web 3000 path/to/common.yml
192.168.99.100:32768

Add this function to your .bashrc file:

function addr {
  service=$1
  containerport=$2
  file=${3:-"docker-compose.yml"}
  echo $(docker-machine ip $(docker-machine active)):$(docker-compose -f $file port $service $containerport | cut -d ':' -f 2)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment