Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read docker tag from container

Tags:

docker

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

like image 659
Dennie de Lange Avatar asked Mar 01 '18 11:03

Dennie de Lange


People also ask

How do I read a Docker label?

To check the labels of a particular Image, you can use the Docker Inspect command. Start the Docker Container. Execute the Inspect Command. Inside the LABELS object, you can find all the labels associated with the image that you have specified inside your Dockerfile.

What is my Docker image tag?

Docker tags are mutable named references to Docker images, much like branch refs in Git. They make it easy to pull and run images, and for image authors to roll out updates automatically. For example, to pull the latest Debian GNU/Linux image for the buster release: $ docker pull debian:buster.

What is Docker tag file?

Docker tags are just an alias for an image ID. The tag's name must be an ASCII character string and may include lowercase and uppercase letters, digits, underscores, periods, and dashes. In addition, the tag names must not begin with a period or a dash, and they can only contain 128 characters.


1 Answers

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.

like image 172
ShayK Avatar answered Sep 29 '22 09:09

ShayK