Home > front end >  Docker commands to store variable values and perform scan operation
Docker commands to store variable values and perform scan operation

Time:06-01

I have a task to perform scan on the docker image and the requirement is :

  1. Pull the docker images from the artifactory.
  2. List the images already downloaded and fetch only the required downloaded image (imageID).
  3. And store it in one variable so that we can use it while running scan.

I have already downloading the image from the artifactory using docker commands-- "docker pull <image_name>" and listing it using-- docker images | grep "$(URL)" | awk '{ print $3 }'

But I am unable to have a variable to find a particular docker imageID which is pulled from the artifactory in this scan run, from the listed down docker images so that I can use that unique variable in my scan command as $(variable_name).

for example: output of command docker images | grep "$(URL)" | awk '{ print $3 }' should be stored in one unique variable so that it will be used for next process and every time pipeline triggerd it should have a fresh value in that stored.

Is there a way to do so.

CodePudding user response:

Assuming you are using bash, you can use:

varname=$(docker images | grep "$(URL)" | awk '{ print $3 }')

However, there are ways to improve on what you're doing. For instance, if you only grep for the URL, the way docker images formats the output is not the same as what is given to docker pull and you can get different incorrect results in a few ways. For example:

$ docker pull ubuntu:22.04
22.04: Pulling from library/ubuntu
Digest: sha256:26c68657ccce2cb0a31b330cb0be2b5e108d467f641c62e13ab40cbec258c68d
Status: Image is up to date for ubuntu:22.04
docker.io/library/ubuntu:22.04
$ docker images | grep ubuntu:22.04  # we get zero hits
$ docker images | grep ubuntu        # we get two hits
ubuntu                                  22.04            d2e4e1f51132   4 weeks ago     77.8MB
ubuntu                                  jammy            f0b07b45d05b   7 weeks ago     77.9MB
$

Instead, use docker images "$URL" --format:

$ URL=ubuntu:22.04
$ docker images "$URL" --format '{{.ID}}'
d2e4e1f51132
$ varname=$(docker images "$URL" --format '{{.ID}}')
$ echo $varname
d2e4e1f51132
  • Related