Skip to content

Instantly share code, notes, and snippets.

@aakbar5
Last active February 13, 2019 16:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aakbar5/8ac6f4447dbb25c7e443dfa88aa9a69e to your computer and use it in GitHub Desktop.
Save aakbar5/8ac6f4447dbb25c7e443dfa88aa9a69e to your computer and use it in GitHub Desktop.
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