GithubHelp home page GithubHelp logo

ryandaniels / docker-script-find-latest-image-tag Goto Github PK

View Code? Open in Web Editor NEW
79.0 5.0 33.0 12 KB

Find Version Tag for Latest Docker Image

Home Page: https://ryandaniels.ca/blog/find-version-tag-latest-docker-image/

License: MIT License

Shell 100.00%
docker docker-scripts

docker-script-find-latest-image-tag's Introduction

docker-script-find-latest-image-tag

(Over complicated) Bash script to find the version of a Docker image you are running when you took a shortcut and used "latest".
Now you don't know what image version was previously working, and a breaking change happened..

docker images shows image:<none> or image:latest. What image version am I using?

Tag your images, and never use latest!

This script checks your local image's ID against every tag of an image in Docker Hub.

Some issues to note:
Issue #1: If the image no longer exists on the Docker Registry. You obviously won't find it.
Issue #2: The script doesn't work for Windows images. There are no manifests available for those.

If you're lucky you can find the version tag

Trick #1

If you're lucky, the image's label will have the version. With a little jq magic you can get the good bits.

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
traefik             latest              96c63a7d3e50        2 months ago        85.7MB

$ IMAGE_ID=96c63a7d3e50

$ docker image inspect --format '{{json .}}' "$IMAGE_ID" | jq -r '. | {Id: .Id, Digest: .Digest, RepoDigests: .RepoDigests, Labels: .Config.Labels}'

Output:

{
  "Id": "sha256:96c63a7d3e502fcbbd8937a2523368c22d0edd1788b8389af095f64038318834",
  "Digest": null,
  "RepoDigests": [
    "traefik@sha256:5ec34caf19d114f8f0ed76f9bc3dad6ba8cf6d13a1575c4294b59b77709def39"
  ],
  "Labels": {
    "org.opencontainers.image.description": "A modern reverse-proxy",
    "org.opencontainers.image.documentation": "https://docs.traefik.io",
    "org.opencontainers.image.title": "Traefik",
    "org.opencontainers.image.url": "https://traefik.io",
    "org.opencontainers.image.vendor": "Containous",
    "org.opencontainers.image.version": "v1.7.20"
  }
}

Trick #2

Pull a bunch of images and hope the Image ID matches. Not much to this trick..

If you are unlucky, continue on..

Requirements

  • Docker or Podman
  • curl
  • jq
  • sed
  • tr
  • grep
  • sort
  • wc

Usage

The Docker image name (-n) must match the name (REPOSITORY) from "docker images". For example "traefik" (which is an "Official" image), or "portainer/portainer".

./docker_image_find_tag.sh -h
Usage:
./docker_image_find_tag.sh [-n image name] [-i image-id]
Example: ./docker_image_find_tag.sh -n traefik -i 96c63a7d3e50 -f 1.7
  -n [text]: Image name (Required). '-n traefik' would reference the traefik image or '-n portainer/portainer' for portainer
  -i [text]: Image ID. Required if Image ID (Long) (-L) is ommited.
             Found in 'docker images'. Can also use -i image:tag
  -L [text]: Image ID (Long). Required if Image ID (-i) is ommited
      Example: -L sha256:96c63a7d3e502fcbbd8937a2523368c22d0edd1788b8389af095f64038318834
      To find, it is Id from:
      docker image inspect --format '{{json .}}' IMAGE_ID | jq -r '. | {Id: .Id, Digest: .Digest, RepoDigests: .RepoDigests, Labels: .Config.Labels}'
  -D: Use Docker binary for Image ID check (Default) (Optional)
  -P: Use Podman binary for Image ID check (Optional)
  -r [text]: Registry URL to use. Example: -r https://index.docker.io/v2 (Default) (Optional)
  -a [text]: Registry AUTH to use. Example: -a https://auth.docker.io (Default) (Optional)
  -l [number]: Tag limit. Defaults to 100. (Optional)
  -f [text]: Filter tag to contain this value (Optional)
  -v: Verbose output (Optional)

Usage Example

# ./docker_image_find_tag.sh -n traefik -i 96c63a7d3e50 -f 1.7. -l 10 -v

Using IMAGE_NAME: traefik
Using REGISTRY: https://index.docker.io/v2
Found Image ID Source: sha256:96c63a7d3e502fcbbd8937a2523368c22d0edd1788b8389af095f64038318834
Found Total Tags: 610
Found Tags (after filtering): 178
Limiting Tags to: 10
Limit reached, consider increasing limit (-l [number]) or use more specific filter (-f [text])

Found Tags:
v1.7.21-alpine
v1.7.21
v1.7.20-alpine
v1.7.20
v1.7.19-alpine
v1.7.19
v1.7.18-alpine
v1.7.18
v1.7.17-alpine
v1.7.17

Checking for image match..
Found match. tag: v1.7.20
Image ID Target: sha256:96c63a7d3e502fcbbd8937a2523368c22d0edd1788b8389af095f64038318834
Image ID Source: sha256:96c63a7d3e502fcbbd8937a2523368c22d0edd1788b8389af095f64038318834

This example is searching for an image named traefik, with an image ID of 96c63a7d3e50 (which we got from running the command docker images.

The total number of tags for traefik images is 610! You could use this script to check every one, but that will take a minute or two. Instead, you can filter the results a bit. I think Iā€™m running something in version 1.7.x. Iā€™m also limiting the search to 10 tags in this example to keep the output small.

docker-script-find-latest-image-tag's People

Contributors

ryandaniels avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

docker-script-find-latest-image-tag's Issues

https://index.docker.io/v2 requests authentication

https://index.docker.io/v2 requests authentication

For example the command:

./docker_image_find_tag.sh -n portainer -i 5526251cc61f -f 2.13. -l 10 -v

outputs:

Using IMAGE_NAME: portainer
Using REGISTRY: https://index.docker.io/v2
Found Image ID Source: sha256:5526251cc61f126f2c1da1875aae113c9af05a5679123dde2f6c15d12754b103

Error 401 from: https://index.docker.io/v2/library/portainer/tags/list
HTTP_BODY: {"errors":[{"code":"UNAUTHORIZED","message":"authentication required","detail":[{"Type":"repository","Class":"","Name":"library/portainer","Action":"pull"}]}]}

use RepoTags field from `docker image inspect` to get associated tags/versions for an image

Hi, I was reading your blog post that goes with this script. If you wanted to get the version associated with an image that is also tagged 'latest', you should be able to do the following...

IMAGEID=sha_of_image
docker image inspect $IMAGEID  | jq -r '.[].RepoTags'

for example, when I do this on a postgres image I have, I get the following...

[
  "postgres:13.2",
  "postgres:latest"
]

Note, this works on most 'mainstream' (ie popular) images as its considered a best practice. If the maintainer doesn't full that, then your script will be needed to try and figure out what 'latest' is.

`parse error: Invalid numeric literal at line 2, column 4`

I'm trying to use docker_image_find_tag.sh, and can't get it to work even with one of your examples:

$ docker_image_find_tag.sh -n traefik -i 96c63a7d3e50 -f 1.7. -l 10 -v
Using IMAGE_NAME: traefik
Using REGISTRY: https://index.docker.io/v2
parse error: Invalid numeric literal at line 2, column 4

I'm running this on a Windows 10 system, using Ubuntu via WSL 2.

At first the script failed because jq wasn't recognized, but once I installed that I couldn't get any further than the error above.

CURL_AUTH error

I got the error when execute this command. thanks!

sh docker_image_find_tag.sh -n mongo -i 87f1a6e84e00

docker_image_find_tag.sh: line 175: CURL_AUTH[@]: unbound variable

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    šŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. šŸ“ŠšŸ“ˆšŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ā¤ļø Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.