<p>As I understand you want to add a variable to the docker image tag, </p>

<p>in this case, you have to get an environment variable from Jenkins and use it in your image label.</p>

<pre><code>    pipeline {
  environment {
    registry = "docker_hub_account/repository_name"
    registryCredential = 'dockerhub'
  }
  agent any
  stages {
    stage('Building image') {
      steps{
        script {
          docker.build registry + ":$BUILD_NUMBER"
        }
      }
    }
  }
}
</code></pre>

<p>pleae use this <a href="https://medium.com/@gustavo.guss/jenkins-building-docker-image-and-sending-to-registry-64b84ea45ee9" rel="nofollow noreferrer">URL</a> for more info</p>

CC BY-SA 4.0
2019-01-28T17:12:38.163Z
No. I don't want to use a pre-existing Docker image from the registry (`docker` agent), I want to build a new Docker image from the Dockerfile in my project (`dockerfile` agent). I know I can use variables, but the problem is that I don't know how to dynamically get the image tag of the newly built image.
Attila Szeremi
CC BY-SA 4.0
2019-01-29T08:35:55.313Z
OK, I will edit my answer
Ahmed Badawy
CC BY-SA 4.0
2019-01-30T15:04:50.410Z
Still not OK. I want to make use of the `dockerfile` agent. You are using the `any` agent now.
Attila Szeremi
CC BY-SA 4.0
2019-01-31T09:37:53.970Z
I think you can't use the agent which you still need to add a tag to it from build number, The Idea from the agent that you have already up and running container connected to Jenkins as a node and it used to build your code ( for your case you need to run docker command so the user agent should have docker command on this agent )
Ahmed Badawy
CC BY-SA 4.0
2021-01-18T11:17:12.163Z
@AhmedBadawy how to pass build argument to docker.build?
Ijaz Ahmad
CC BY-SA 4.0
2019-01-28T16:23:02.187Z
2019-01-29T08:48:24.943Z
2019-01-29T08:48:24.943Z
Ahmed Badawy
<p><code>docker build</code> <em>does</em> output the image ID:</p>

<pre><code>Successfully built 6fd2b9d56348
Successfully tagged foo:latest
</code></pre>

<p>Unfortunately, it doesn't seem like the <a href="https://github.com/jenkinsci/docker-build-step-plugin" rel="nofollow noreferrer">Docker Build Step plugin</a> exposes it to the pipeline (it <em>does</em> write it to the log!). You may want to open an issue with that project.</p>

<p>That said, <code>docker tag</code> itself accepts <code>name:tag</code>:</p>

<pre><code>$ docker help tag
Usage:  docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
</code></pre>

<p>Since you (can) choose image name and first tag for <code>docker build</code>, try using those to identify the built image instead of its ID. If that doesn't work with the plugin, you may need a feature request after all.</p>

<p>As a workaround, you can always use <code>sh script: 'docker ...'</code> directly.</p>

CC BY-SA 4.0
2019-02-28T14:27:21.647Z
2019-02-28T14:27:21.647Z
Raphael
<p>Probably you need <code>additionalBuildArgs</code> to tag your image as <code>mytag</code>:</p>
<pre class="lang-java prettyprint-override"><code>    agent {
        dockerfile {
            additionalBuildArgs '-t mytag'
        }
    }
</code></pre>

CC BY-SA 4.0
2021-06-22T13:53:38.037Z
2021-06-22T13:53:38.037Z
Alexey Vazhnov
<p>I'm trying to get the image tag generated with the help of the dockerfile agent in Jenkins declarative pipelines.</p>

<p>This Jenkinsfile is incomplete though, because I still need the deployment steps.</p>

<p>Normally, people would then want a step to push to Dockerhub. Although I don't even know how to do that, I want something different: <strong>I want to re-tag the built image locally</strong> to a name of my choice. This is because this is a hobby project, and my Jenkins and production servers are the same. The <strong>problem is that I don't know how to get the dynamically generated image ID</strong> for the <code>docker tag</code> command.</p>

<p>(Then what would come next is to swap out the old running image with the new one, but I could probably take care of that myself.)</p>

<p>Here is my Jenkinsfile:</p>

<pre><code>pipeline {
    agent {
        dockerfile {
            args '--network szeremi -e DB_HOST=mysql -e APP_ENV=testing'
        }
    }

    stages {
        stage('Run tests') {
            steps {
                sh 'cd /var/www &amp;&amp; php artisan migrate:fresh &amp;&amp; vendor/bin/phpunit'
            }
        }
    }
}
</code></pre>

<p>Please help me use Jenkins declarative pipelines to re-tag my built-by-dockerfile image.</p>

<docker><jenkins><jenkins-pipeline><dockerfile>
Jenkins Dockerfile declarative pipelines: getting the built image tag
Attila Szeremi
# Jenkins Dockerfile declarative pipelines: getting the built image tag

(Asked by: username_0 on 2019-01-28)

I'm trying to get the image tag generated with the help of the dockerfile agent in Jenkins declarative pipelines.
This Jenkinsfile is incomplete though, because I still need the deployment steps.
Normally, people would then want a step to push to Dockerhub. Although I don't even know how to do that, I want something different: I want to re-tag the built image locally to a name of my choice. This is because this is a hobby project, and my Jenkins and production servers are the same. The problem is that I don't know how to get the dynamically generated image ID for the docker tag command.
(Then what would come next is to swap out the old running image with the new one, but I could probably take care of that myself.)
Here is my Jenkinsfile:
pipeline {
    agent {
        dockerfile {
            args '--network szeremi -e DB_HOST=mysql -e APP_ENV=testing'
        }
    }

    stages {
        stage('Run tests') {
            steps {
                sh 'cd /var/www && php artisan migrate:fresh && vendor/bin/phpunit'
            }
        }
    }
}

Please help me use Jenkins declarative pipelines to re-tag my built-by-dockerfile image.

**Tags:** docker, jenkins, jenkins-pipeline, dockerfile


---

## 3 answers

### Answer by username_1 on 2019-01-28. Score: 1

As I understand you want to add a variable to the docker image tag, 
in this case, you have to get an environment variable from Jenkins and use it in your image label.
    pipeline {
  environment {
    registry = "docker_hub_account/repository_name"
    registryCredential = 'dockerhub'
  }
  agent any
  stages {
    stage('Building image') {
      steps{
        script {
          docker.build registry + ":$BUILD_NUMBER"
        }
      }
    }
  }
}

pleae use this URL for more info

> **Comments:**
> **username_0**: No. I don't want to use a pre-existing Docker image from the registry (`docker` agent), I want to build a new Docker image from the Dockerfile in my project (`dockerfile` agent). I know I can use variables, but the problem is that I don't know how to dynamically get the image tag of the newly built image.  
> **username_1**: OK, I will edit my answer  
> **username_0**: Still not OK. I want to make use of the `dockerfile` agent. You are using the `any` agent now.  
> **username_1**: I think you can't use the agent which you still need to add a tag to it from build number, The Idea from the agent that you have already up and running container connected to Jenkins as a node and it used to build your code ( for your case you need to run docker command so the user agent should have docker command on this agent )  
> **username_2**: @AhmedBadawy how to pass build argument to docker.build?  

---

### Answer by username_3 on 2019-02-28. Score: 1

docker build does output the image ID:
Successfully built 6fd2b9d56348
Successfully tagged foo:latest

Unfortunately, it doesn't seem like the Docker Build Step plugin exposes it to the pipeline (it does write it to the log!). You may want to open an issue with that project.
That said, docker tag itself accepts name:tag:
$ docker help tag
Usage:  docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]

Since you (can) choose image name and first tag for docker build, try using those to identify the built image instead of its ID. If that doesn't work with the plugin, you may need a feature request after all.
As a workaround, you can always use sh script: 'docker ...' directly.


---

### Answer by username_4 on 2021-06-22. Score: 0

Probably you need additionalBuildArgs to tag your image as mytag:
    agent {
        dockerfile {
            additionalBuildArgs '-t mytag'
        }
    }


---

