Dennie de Lange
Dennie de Lange

Reputation: 2934

Read docker tag from container

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>.

Upvotes: 14

Views: 4651

Answers (1)

ShayK
ShayK

Reputation: 429

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.

Upvotes: 4

Related Questions