Skip to content

Instantly share code, notes, and snippets.

@miclip
Last active November 13, 2019 06:13
Show Gist options
  • Save miclip/7f2ab0b37c5dee8a63981139aba0ae80 to your computer and use it in GitHub Desktop.
Save miclip/7f2ab0b37c5dee8a63981139aba0ae80 to your computer and use it in GitHub Desktop.

Run docker build on Jenkins deployed on PKS using Host docker.sock

Update deployment with:

Make container privileged.

securityContext:
      privileged: true

Create a VolumeMount for /var/run/docker.sock

volumeMounts:
  - mountPath: /tmp
    name: tmp
  - mountPath: /var/jenkins_home
    name: jenkins-home
  - mountPath: /var/jenkins_config
    name: jenkins-config
    readOnly: true
  - mountPath: /usr/share/jenkins/ref/secrets/
    name: secrets-dir
  - mountPath: /usr/share/jenkins/ref/plugins/
    name: plugin-dir
    
  - mountPath: /var/run/docker.sock
    name: docker-socket-volume
    
  - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
    name: jenkins-token-v4m4w
    readOnly: true

Create hostpath volume to location of docker.sock on host vm. e.g. /var/vcap/sys/run/docker/docker.sock

    volumes:
    - emptyDir: {}
      name: plugins
    - emptyDir: {}
      name: tmp
    - configMap:
        defaultMode: 420
        name: jenkins
      name: jenkins-config
    - emptyDir: {}
      name: secrets-dir
    - emptyDir: {}
      name: plugin-dir
    - name: jenkins-home
      persistentVolumeClaim:
        claimName: jenkins
        
    - hostPath:
        path: /var/vcap/sys/run/docker/docker.sock
        type: File
      name: docker-socket-volume
      
    - name: jenkins-token-v4m4w
      secret:
        defaultMode: 420
        secretName: jenkins-token-v4m4w

Ensure POD is redeployed.

Jenkins example pipeline usage:

cp /var/jenkins_home/workspace/spring-boot-service/target/gs-rest-service-0.1.0.jar /var/jenkins_home/workspace/spring-boot-service/
docker build -t harbor.run.pivotal.io/myrepo/spring-boot-service . --build-arg JAR_FILE=gs-rest-service-0.1.0.jar
docker login https://harbor.run.pivotal.io/myrepo -u $HARBOR_USERNAME -p $HARBOR_PASSWORD
docker push harbor.run.pez.pivotal.io/myrepo/spring-boot-service

Note that the jar must be copied to the same directory as the Dockerfile so it's packaged and provided to the docker daemon on the host vm.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment