Docker Community Forums

Share and learn in the Docker community.

How to find out container version?

I’m looking for a way to find out a container’s version.
% docker ps will show me the tag, but the tag is “latest”, that WAS the latest when I downloaded the container but might not be the latest anymore. So how do I know what is the version of my running constainer? :slight_smile:

There is no such thing as a container’s version.

You can identify an exact image by it’s sha256 digest, use docker image inspect ${image id} --format '{{.RepoDigests}}' to get the digest. If you are lucky, the digest is additionaly referenced by a immutable tag, which in theory might help you to identify the “version” on Dockerhub (manual task)

Though, some maintainers add labels or environment variables to transport version information in their images. It’s a good habit, but nothing that can be expected to be generally available. Use `docker history ${image digest or id}

Thank you!
In this case what is the recommended method to find out if I’m running the latest version of a container? How do I know if I should update or not?

The latest tag usualy is mutable, thus it points to the recent image’s sha256 digest.

Execute docker pull imagename:tag to pull the latest image for the tag. If it pulls no layer, you can be sure that you already have the current image in your local image cache.

A container is bound to a specific image sha256 digest. You need to remove the container that points to the old image and re-create a new container that points to the current image in your local image cache (this is where all images from docker pull and docker build are stored). All data not persisted in volumes/bind mounts will be lost. Beginners tend to forget them sometimes and are surpised why the last their data…

Thank you!
Let’s say there is a new image but it doesn’t work and I have to roll back, how would I do it? How do I know to which version to roll back?

Assumed you still have the “old” image for the tag in your local image cache.
Inspect the old image (for the repository having the tag <none>) with docker image inspect ${image id} --format '{{.RepoDigests}}', then use the returned value (=the RepoDigest) without the wrapping [ and ] charachters in your docker run command or docker-compose.yml instead of the image:tag.

If you don’t use docker-compose yet, it is highly recommended to use it.

update: bloody forum markup is interpreting <none> as html tag which make it invisible - had to wrap it in backticks to keep it visible.

1 Like

That’s awesome, thank you!

Get an instance’s IP address
For the most part, you can pick out any field from the JSON in a fairly straightforward manner.

$ docker inspect --format=’{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}’ INSTANCE_ID Get an instance’s MAC address docker inspect --format=’{{range .NetworkSettings.Networks}}{{.MacAddress}}{{end}}’ INSTANCE_ID Get an instance’s log path docker inspect --format=’{{.LogPath}}’ INSTANCE_ID Get an instance’s image name docker inspect --format=’{{.Config.Image}}’ $INSTANCE_ID
List all port bindings
You can loop over arrays and maps in the results to produce simple text output:

$ docker inspect --format=’{{range $p, $conf := .NetworkSettings.Ports}} {{$p}} -> {{(index $conf 0).HostPort}} {{end}}’ $INSTANCE_ID
Find a specific port mapping
The .Field syntax doesn’t work when the field name begins with a number, but the template language’s index function does. The .NetworkSettings.Ports section contains a map of the internal port mappings to a list of external address/port objects. To grab just the numeric public port, you use index to find the specific port map, and then index 0 contains the first object inside of that. Then we ask for the HostPort field to get the public address.

$ docker inspect --format=’{{(index (index .NetworkSettings.Ports “8787/tcp”) 0).HostPort}}’ $INSTANCE_ID
Get a subsection in JSON format
If you request a field which is itself a structure containing other fields, by default you get a Go-style dump of the inner values. Docker adds a template function, json, which can be applied to get results in JSON format.

$ docker inspect --format=’{{json .Config}}’ $INSTANCE_ID

a classic lewish95 response :rofl:
@dlaci: You are welcome! And please ignore the response from lewish95. This user is high likely someones missguided AI playground.

1 Like

If you care about version management of your app or an image that it depends on then NEVER use the the ‘latest’ tag. There is no way you can tell which version you are actually using and whether it’s behaviour is likely to introduce a breaking change. Always using explicit tags, both for your own images and any you use from a public registry. That way you can run multiple versions, roll backwards and forwards, implement blue-green update strategies, do canary testing … and so on. All of that is impossible to do reliably using ‘latest’.

Thank you goffinf, sounds good

goffinf, thanks for explanation. Our company has been facing almost the same technical problems and issues, which appear time to time, and yes, your method helps sometimes. But sometimes you need way more reliable help from professionals, like this guys from here ( link: https://sphereit.uk ), because they definitely know better what to do in such situations, and know how to resolve any technical issue on the servers of your company.