Docker Community Forums

Share and learn in the Docker community.

Get a container's full id from inside of itself


(Jfgineste) #1


For a project, I absolutely need to know a container’s full id from inside of itself (without executing any command on the host). Is there any way to get this information?

For information, this is the function which I use from the host to get this information, which is obviously unusable from inside of a container.

function docker_full_id(){
  docker inspect $(docker ps | grep "$1" | awk '{print $1}') | grep '"Id":' | awk '{print $2}' | tr --delete '"' | tr --delete ','

Many thanks !


(Jfgineste) #2

Well, it seems that I already found the solution !
I simply have to run the following command from inside a container

cat /proc/self/cgroup | head -1 | tr --delete ‘10:memory:/docker/’

Best regards

(Siju Oommen George) #3

cat /proc/self/cgroup | grep -o -e “docker-..scope" | head -n 1 | sed "s/docker-(.).scope/\1/”

(Danielibarnes) #4

head -1 /proc/self/cgroup|cut -d/ -f3

(Majinkhoo) #5

is there a bash command for getting the container name instead of the id inside docker?