简体   繁体   中英

How to get Image ID of docker in jenkins?

I need to run docker container through Jenkins. For that I used " Start/Stop Docker Containers " as build step

Start/Stop Docker Containers

Action to choose        : Run container
Docker Cloud name       : docker_demo
ID                      : DOCKER_IMAGE_ID
DNS     
Port bindings           :port
Bind all declared ports :   
Hostname                : ip address

While running the job,it is telling that "Invalid ID". Can anyone suggest,How to get Image ID of docker in jenkins??

One liner based on docker documentation :

sudo docker images --filter=reference=image_name --format "{{.ID}}"

Replace image_name by your actual docker image name. You can store that value in a shell variable for further referencing with:

IMAGE_ID=$(sudo docker images --filter=reference=image_name --format "{{.ID}}")

And then access it with $IMAGE_ID

A simpler way to solve it:

docker images --filter="reference=your_image_name" --quiet

What this command means:

  • --filter : Search a image based on name (represented by your_image_name )
  • --quiet : Return only the image ID

Reference: https://docs.docker.com/engine/reference/commandline/images/

You can see the docker image id (for the images you have built or pulled from docker hub) using the following command: docker images

Update after comments from OP:

docker images --filter only implements dangling=true so you can't search for container ids using this way. so you'll have to rely on shell scripting, here's an example:

docker images | grep -E '^golang.*latest' | awk -e '{print $3}' 

You'll need to tailor the regex in the grep to match your image name and tag.

docker ps -a --format "{{.ID}}" | head -1

Placeholder

.ID

.Repository

.Tag

.Digest

.CreatedSince

.CreatedAt

.Size

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM