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.
Written by Ganeshwara Herawan Hananda Putra
Related protips
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Shell
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#