Last Updated: February 25, 2016
·
377
· lolski

Shell alias for getting the ip address of a docker container easily

Add the following at the very end of your shell init script, e.g. ~/.bashrc or ~/.zshrc:

docker_ipof() {
  docker inspect $1| grep PAddr | cut -d '"' -f 4
}
alias ipof=docker_ipof`

and then you can use ipof <container-name | container-id> in order to return its IP address.

Example:

$ docker run --name -d node1 phusion/baseimage /sbin/my_init --enable-insecure-key
$ ssh -i insecure_key root@`ipof node1` # container ID prefix (e.g. c323) works too!

EDIT:

changed cut -c23-32 to cut -d '"' -f 4 which simply does a better job.