How to build docker images in kubernetes with Jenkins without privileges using img

Monday, Sep 16, 2019| Tags: kubernetes, CICD, jenkins, docker, img, devops, rootless

Note: I will be using an Amazon EKS cluster on AWS and using Amazon ECR for storing images. There will be minor differences for handling other repositories.

In the earlier post we learnt on how to to build a docker image using kubernete plugin in Jenkins on Kuberenetes using kaniko. In this post we will look at how to build a docker image using Img. We will follow the same steps as the earlier post but will have a different pipeline script.

Img can help make your life easier when it comes to building container images.

We will build a Jenkins pipeline that will be responsible for pulling code, building image and pushing image to Amazon ECR.

If you don’t already have Jenkins installed then follow the steps in this post

We will follow the below steps:

  1. Create a configmap for docker configuration that will use ECR credential helper
  2. Build a Jenkins pipeline

Step 1: Create a configmap for docker configuration that will use ECR credential helper

Amazon ECR uses AWS IAM authentication to get docker credentials for pushing the images. ECR crdenetial helper makes getting the credentials for pushing images easier. Setting up ECR crdenetial helper for Docker/Kaniko needs a configuration file. Let’s go ahead and create a configuration file.

Create a configmap docker-config.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
apiVersion: v1
kind: ConfigMap
metadata:
  name: docker-config
data:
  config.json: |-
    {
      "credHelpers": {
        "123456789498.dkr.ecr.us-west-2.amazonaws.com": "ecr-login"
      }
    }


Replace 123456789498 with your AWS account number.

Run the below command to create the configmap. You must install the configmap in the same namespace where jenkins is installed.

kubectl -n jenkins apply -f docker-config.yaml

Step 2: Create a Jenkins pipeline to build and push the container image

Once you are logged in to Jenkins it’s time to create a new Jenkins pipeline. Follow the steps:

1. Create a New Item

2. Create a new Pipeline

3. Place the pipeline script in the job

Now place the below script in the pipeline script section:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
pipeline {
  agent {
    kubernetes {
      //cloud 'kubernetes'
      yaml """
kind: Pod
metadata:
  name: img
spec:
  containers:
  - name: img
    image: jessfraz/img
    imagePullPolicy: Always
    command:
    - cat
    tty: true
    volumeMounts:
      - name: docker-config
        mountPath: /home/user/.docker
  volumes:
    - name: docker-config
      configMap:
        name: docker-config
"""
    }
  }
  stages {
    stage('Build with Img') {
      environment {
        PATH = "/home/user/bin:$PATH"
      }
      steps {
        git 'https://github.com/prabhatsharma/sample-microservice'
        container(name: 'img') {
            sh 'wget https://amazon-ecr-credential-helper-releases.s3.us-east-2.amazonaws.com/0.3.1/linux-amd64/docker-credential-ecr-login'
            sh 'chmod +x docker-credential-ecr-login'
            sh 'mkdir ~/bin'
            sh 'mv docker-credential-ecr-login ~/bin/docker-credential-ecr-login'
            sh '''
            img build . -t 12345694928.dkr.ecr.us-west-2.amazonaws.com/sample-microservice:latest -t 12345694928.dkr.ecr.us-west-2.amazonaws.com/sample-microservice:vImg$BUILD_NUMBER
            '''
            sh ' img push 12345694928.dkr.ecr.us-west-2.amazonaws.com/sample-microservice:latest'
            sh ' img push 12345694928.dkr.ecr.us-west-2.amazonaws.com/sample-microservice:vImg$BUILD_NUMBER'
        }
      }
    }
  }
}


We are using a sample microservice for which we will build an image and push it to ECR. You must also have the 123456789498.dkr.ecr.us-west-2.amazonaws.com/sample-microservice ECR repository created before running this pipeline.

Pay close attention from line 35 to 38 where we are downloading Amazon ECR credential helper and placing it in the container. We did not have to do this with kaniko, as its docker image comes bundled with ecr (and gcr) credential helpers.


Now save the pipeline. You are all set up. Next you can click Build Now link to start the build.

Once the build completes your screen should look like below

and you should have a docker image in your repository:

You are all set !!!


You would want to use img instead of kaniko as jenkins kubernetes plugin is compatible with latest version of img docker image. The best part of img is that it does not need root privileges to build images.

Happy building images!!!



Comments