Docker Community Forums

Share and learn in the Docker community.

Exposing image id and hostname in the container's environment?

(Kai Hendry) #1

Hi there,

I would like to see the image id and the hostname of the host from my Web application. Has anyone achieved that?

I noticed HOSTNAME is the container id, which is moderately useful, but I would like more please.

(Theodoregoetz) #2

I too have been looking for a way to get the image ID from within the running container. Best I could come up with is to give the container access to the docker socket, install docker into the container and use that.

Mount docker socket:


Make sure container has docker installed and run this:

[edit: here’s a way to bypass the image name (kept below for posterity)]

DOCKER_CONTAINER_ID=$(cat /proc/self/cgroup | grep 'docker-' | tail -1 | awk -F'[-.]' '{print $3}')
    DOCKER_IMAGE_ID=$(docker inspect -f '{{.Image}}' $DOCKER_CONTAINER_ID | awk -F: '{print $NF}')

Obviously fragile, but I’d be interested in a better way!

older method:

DOCKER_CONTAINER_ID=`cat /proc/self/cgroup | grep "docker-" | tail -1 | awk -F'[-.]' '{print $3}'`
DOCKER_IMAGE_NAME=`docker ps -f id=$DOCKER_CONTAINER_ID --format "{{.Image}}"`
DOCKER_IMAGE_ID=`docker images -q --no-trunc $DOCKER_IMAGE_NAME | awk -F: '{print $NF}'`