<p>Currently I am trying to add version number or build number for Docker image to deploy on Kubernetes cluster. Previously I was working only with <code>:latest</code> tag. But when I am using <code>latest</code> tag , I found problem for pulling from Dockerhub image registry. So when I am using the build number to my docker image like <code>&lt;image-name&gt;:{build-number}</code> . </p>

<h2>Application Structure</h2>

<p>In my Kubernetes deployment, I am using deployment and service. I am defining my image repository in my deployment file like the following,</p>

<pre><code>   containers:
   - name: test-kube-deployment-container
     image: samplekubernetes020/testimage:latest
     ports:
        - name: http
          containerPort: 8085
          protocol: TCP
</code></pre>

<p>Here instead of latest tag, I want to put build number with my image in deployment YAML.</p>

<ol>
<li>Can I use an environment variable for holding the random build number for accessing like <code>&lt;image-name&gt;:${buildnumber}</code> ?</li>
<li>If i want to use a environment variable which providing the random number how I can generate a random number to a environment variable?</li>
</ol>

<p><strong>Updates On Image Version Implementation</strong></p>

<p>My modified Jenkinsfile contains the step like following to assign the image version number to image. But still I am not getting the updated result after changes to repository,</p>

<p>I created step like the following in Jenkinsfile</p>

<pre><code>stage ('imagebuild')
  {
     steps
         {
 sh 'docker build -f /var/lib/jenkins/workspace/jpipeline/pipeline/Dockerfile -t spacestudymilletech010/spacestudykubernetes /var/lib/jenkins/workspace/jpipeline/pipeline'
 sh 'docker login --username=my-username --password=my-password' 
 sh "docker tag spacestudymilletech010/spacestudykubernetes:latest spacestudymilletech010/spacestudykubernetes:${VERSION}"
 sh 'docker push spacestudymilletech010/spacestudykubernetes:latest'

         }
  }
</code></pre>

<p>And my deployment YAML file contains like the following,</p>

<pre><code> containers:
   - name: test-kube-deployment-container
     image: spacestudymilletech010/spacestudykubernetes:latest
     ports:
        - name: http
          containerPort: 8085
          protocol: TCP
</code></pre>

<p><strong>Confusions</strong> </p>

<p><strong>NB:</strong> When I am checking the dockerhub repository, every time it showing the latest push status</p>

<p>So my confusions are:</p>

<ol>
<li>Is there any problem with pulling latest image in my deployment.yaml file?</li>
<li>Is the problem when I am tagging the image at my machine from where I am building the image and pushing?</li>
</ol>

Mr.DevEng
<p>The standard way or at least the way that has worked for most of us is to create versioned or tagged images. For example</p>
<pre><code>samplekubernetes020/testimage:1
samplekubernetes020/testimage:2
samplekubernetes020/testimage:3
...
...
</code></pre>
<p>Now I will try to answer your actual question which is <em>how do I update the image which is in my deployment when my image tag upgrades?</em></p>
<p><strong>Enter Solution</strong></p>
<ul>
<li><p>When you compile and build a new image with latest version of code, <strong>tag it</strong> with an incremental unique version. This tag can be anything unique or build number, etc.</p>
</li>
<li><p>Then push this tagged image to docker registry</p>
</li>
<li><p>Once the image is uploaded, this is when you can use <code>kubectl</code> or kubernetes API to update the deployment with the latest container image.</p>
<p><code>kubectl set image deployment/my-deployment test-kube-deployment-container=samplekubernetes020/testimage:1 --record</code></p>
</li>
<li><p>The above set of steps generally take place in your CI pipeline, where you store the image version or the image: version in the environment variable itself.</p>
</li>
</ul>
<hr />
<p><strong>Update Post comment</strong></p>
<p>Since you are using Jenkins, you can get the current <em>build number</em> and <em>commit-id</em> and many other variables in Jenkinsfile itself as Jenkins injects these values at builds runtime. For me, this works. Just a reference.</p>
<pre><code>environment { 
   NAME = &quot;myapp&quot;
   VERSION = &quot;${env.BUILD_ID}-${env.GIT_COMMIT}&quot;
   IMAGE = &quot;${NAME}:${VERSION}&quot;
}
stages {
    stage('Build') {
        steps {
            echo &quot;Running ${VERSION} on ${env.JENKINS_URL}&quot;
            git branch: &quot;${BRANCH}&quot;,  .....
            echo &quot;for brnach ${env.BRANCH_NAME}&quot;
            sh &quot;docker build -t ${NAME} .&quot;
            sh &quot;docker tag ${NAME}:latest ${IMAGE_REPO}/${NAME}:${VERSION}&quot;
        }
    }
}
</code></pre>
