Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 805
Jithin Justice Avatar asked Jul 07 '15 08:07

Jithin Justice


People also ask

How do I build a dockerfile using Jenkins?

This Dockerfile is built from Jenkins official image, install docker and give access to user jenkins build dockers. To get the jenkins docker build, let's run the following command: $ docker build -t jenkins-with-docker .

How to find the Docker image ID?

You can see the docker image id (for the images you have built or pulled from docker hub) using the following command: docker images 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:

Can a jenkinsfile run MySQL as a sidecar in Docker?

Using the withRun method, implemented in the Docker Pipeline plugin’s support for Scripted Pipeline, a Jenkinsfile can run MySQL as a sidecar: This example can be taken further, utilizing two containers simultaneously. One "sidecar" running MySQL, and another providing the execution environment, by using the Docker container links.

How do I build an image from a dockerfile?

To build the image on your own computer, navigate to the project directory (the one with your application code and the Dockerfile), and run docker build: docker build . -t getintodevops-hellonode:1 This instructs Docker to build the Dockerfile in the current directory with the tag getintodevops-hellonode:1.


2 Answers

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

like image 144
amiabl Avatar answered Oct 22 '22 06:10

amiabl


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/

like image 35
Thais Marinho Avatar answered Oct 22 '22 04:10

Thais Marinho