14

I know it is possible to access the docker api and the following command works just fine:
curl -s --unix-socket /var/run/docker.sock http:/v1.24/containers/$HOSTNAME/json | jq -r '.Image'

However, I would really like avoid exposing the docker.sock to the container itself because it is part of a CI build. Is there any other way of retrieving the container image id / hash (i.e. 2acdef41a0c) from within a container itself without exposing the docker.sock and making a curl request to it?

Maybe something like what's shown here Docker, how to get container information from within the container ?

tftd
  • 16,203
  • 11
  • 62
  • 106
  • if you are in gitlab-ci you have these variables available https://docs.gitlab.com/ce/ci/variables/README.html – Mazel Tov Mar 15 '18 at 20:38
  • if you need to get the image hash you could pass it to docker container on startup with ENV variable – Mazel Tov Mar 15 '18 at 20:40
  • It's running in Jenkins via Docker Swarm plugin. What would you pass in the `--env` that would have this? – tftd Mar 15 '18 at 20:48
  • I mean, if it's a shell command you could do `docker images ...` and parse the hash, but in my case I only have a field in which I can add manually `--env` arguments which are not executed through bash/shell AFAIK. (https://github.com/jenkinsci/docker-swarm-plugin) – tftd Mar 15 '18 at 20:51
  • i see, you can pass it to ENV file during buildtime though if you are in CI – Mazel Tov Mar 15 '18 at 20:55
  • maybe these variables will help https://wiki.jenkins.io/display/JENKINS/Building+a+software+project – Mazel Tov Mar 15 '18 at 20:59
  • I'm not building the image - it's already built. I'm using the image to provision an agent for Jenkins via the Docker Swarm Plugin. Unfortunately I can't seem to find any variables which are capable of displaying this information - not even in the Pipeline Syntax pages. That's why I'm rather looking for a "workaround" to this issue. – tftd Mar 15 '18 at 21:02

3 Answers3

3

This sets IMAGE env var inside a container:

docker run --rm -it -e IMAGE=$(docker images ubuntu:bionic --format {{.ID}}) ubuntu:bionic
ShayK
  • 429
  • 3
  • 6
  • Interesting idea, but this really doesn't work with CI servers, because the container is provisioned by the CI server and you can't "just" execute the command above. Even if you could, this would mean you would need to create an insane amount of agent templates for each image you use which would be impractical. – tftd Aug 20 '21 at 21:46
0

The same as @Shayk response, but using docker SDK (python) https://docker-py.readthedocs.io/

import docker
....
client = docker.from_env()
image_name = "ubuntu:bionic"
image_id = str(client.images.get(image_name).id).split(":")[1][:12]

container = client.containers.run(image_name, 
    network = "host",
    environment=[f"IMAGE_ID={image_id}",f"IMAGE_NAME={image_name}"],
    detach=True)

nguaman
  • 925
  • 1
  • 9
  • 23
-1

The following command run inside the container should give you the container ID or the docker image hash- cat /proc/self/cgroup | head -n 1 | cut -d '/' -f3 | cut -c 1-12

AmitP
  • 419
  • 4
  • 9