How to get Image ID of docker in jenkins?
Asked Answered
H

4

12

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

Highclass answered 7/7, 2015 at 8:18 Comment(3)
Isn't it just the image name like ubuntu:latest?Gynarchy
Nope.Actually i created one image(which is created while execution time of jenknis job) using docker.repositry(local).Now I need to retrieve that image ID.So how to retrieve it??Highclass
Have you solved this problem yet? It seems from the question you may be confused about the difference between a container and an image.Antler
G
22

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

Gora answered 12/12, 2018 at 13:36 Comment(2)
don't forget to close parenthesis at the end: IMAGE_ID=$(docker images --filter=reference=image_name --format "{{.ID}}")Teerell
Thanks for the heads-up @Tabaraei, I fixed it.Gora
M
12

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/

Mush answered 19/8, 2020 at 12:0 Comment(1)
--no-trunc is also needed hereDevolution
T
3

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.

Trygve answered 7/7, 2015 at 11:28 Comment(2)
yup.that way i did.but it is static.(every time i need to change the id,if content of image is modified).I need a dynamic id(some sort of variable) so that it will automatically fetch the updated image id from the repository.Highclass
docker images --filter now supports searching by reference $ docker images --filter=reference='busy*:*libc' and also format to choose format of the output see the documentationDasya
H
0
docker ps -a --format "{{.ID}}" | head -1

Placeholder

.ID

.Repository

.Tag

.Digest

.CreatedSince

.CreatedAt

.Size

Haff answered 26/3, 2019 at 15:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.