title: How to version docker images with build number in Jenkins to deploy as Kubernetes deployment?
body: <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>