Skip to content

Instantly share code, notes, and snippets.

@aakbar5
Last active Feb 13, 2019
Embed
What would you like to do?
Find docker host name, ip and id inside the docker container.
# Incase you are interested to find out docker short, full id, hostname and ip address
# of the running container, execute following commands from inside of your container
# To find out docker container id (short form)
DOCKER_ID=`cat /etc/hostname`
echo $DOCKER_ID
# To find out docker container host name using /etc/hosts
HOST_NAME=`grep $DOCKER_ID /etc/hosts | sed 's/\s/\n/g' | tail -1`
# - grep: search for the line having docker id
# - sed: tokenize the previously found line by replacing space with newline character
# - tail: grab the last line which is having host name
HOST_IP=`grep $DOCKER_ID /etc/hosts | sed 's/\s/\n/g' | head -1`
# - Same as above however this time it uses head command to grab the
# first line which is having ip address assigned to the docker container
# To find out docker container id (long form or full id)
FULL_DOCKER_ID=`cat /proc/self/cgroup | grep "cpu:/docker/" | sed 's/\//\n/g' | tail -1`
# - cat: print ouput of /proc/self/cgroup
# - grep: filter line having cpu:/docker/
# - sed: tokenize the previously found line by replacing / with newline character
# - tail: grab the last line which is having docker id
# To find out ip address assigned to docker0 on host machine
DOCKER_0_IP=ifconfig docker0 | grep 'inet' | sed -e 's/^[ \t]*//' | sed 's/\s\+/\n/g' | head -2 | tail -1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment