13

Is it possible to get the current image tag inside a running container? Currently I am passing an environment variable with the same name as the tag, but it would be nice if I could somehow read it from a docker supplied environment variable.

<name>/<image>:<tag>

I am doing a sed in a config based on the <tag>.

Dennie de Lange
  • 2,776
  • 2
  • 18
  • 31
  • Maybe related: [Accessing tag as an environment variable inside a Docker container](https://stackoverflow.com/questions/28128649/accessing-tag-as-an-environment-variable-inside-a-docker-container) – tgogos Mar 01 '18 at 11:31

1 Answers1

4

If you don't mind adding curl and jq to the container and also mounting the docker socket , you can retrieve the image by running the following script inside the container:

#!/bin/bash

CONTAINER_ID=$(head -1 /proc/self/cgroup | rev | cut -d/ -f 1 | rev)
curl --unix-socket /var/run/docker.sock http:/v1.40/containers/{$CONTAINER_ID}/json | jq .Config.Image

The first line gets the container id from /proc/self/cgroup and the second uses docker api to inspect the container.

ShayK
  • 429
  • 3
  • 6
  • 1
    Could it be there's something wrong with the URL you're passing to curl? I get an error `curl: (7) Couldn't connect to server`. I've changed it to `http://docker:2375/v1.40...` as it's something I see recurring on the internet, but to the same effect. – Peter Apr 27 '20 at 07:41
  • Mounting Docker socket is of high-security risk. You give VM root access to this particular container. – mierzwid Mar 08 '21 at 12:58
  • @mierzwid is your comment regarding `http://docker:2375/v1.40...` in the first comment, or `http:/v1.40/containers...` in the answer? Just so I can be sure which one _shouldn't_ be used because it "give[s] VM root access to this particular container" – Henry Blyth Dec 10 '21 at 16:41
  • My comment is regarding starting the Docker container in privilege mode, when it has access to the /var/run/docker.sock. Access to docker.sock == root access to the machine. – mierzwid Dec 13 '21 at 09:34