2

I'm running a jenkins instance on GCE inside a docker container and would like to execute a multibranch pipeline from this Jenkinsfile and Github. I'm using the GCE jenkins tutorial for this. Here is my Jenkinsfile

node {
  def project = 'xxxxxx'
  def appName = 'gceme'
  def feSvcName = "${appName}-frontend"
  def imageTag = "eu.gcr.io/${project}/${appName}:${env.BRANCH_NAME}.${env.BUILD_NUMBER}"

  checkout scm

  sh("echo Build image")
  stage 'Build image'
  sh("docker build -t ${imageTag} .")

  sh("echo Run Go tests")
  stage 'Run Go tests'
  sh("docker run ${imageTag} go test")

  sh("echo Push image to registry")
  stage 'Push image to registry'
  sh("gcloud docker push ${imageTag}")

  sh("echo Deploy Application")
  stage "Deploy Application"
  switch (env.BRANCH_NAME) {
    // Roll out to canary environment
    case "canary":
        // Change deployed image in canary to the one we just built
        sh("sed -i.bak 's#eu.gcr.io/cloud-solutions-images/gceme:1.0.0#${imageTag}#' ./k8s/canary/*.yaml")
        sh("kubectl --namespace=production apply -f k8s/services/")
        sh("kubectl --namespace=production apply -f k8s/canary/")
        sh("echo http://`kubectl --namespace=production get service/${feSvcName} --output=json | jq -r '.status.loadBalancer.ingress[0].ip'` > ${feSvcName}")
        break

    // Roll out to production
    case "master":
        // Change deployed image in canary to the one we just built
        sh("sed -i.bak 's#eu.gcr.io/cloud-solutions-images/gceme:1.0.0#${imageTag}#' ./k8s/production/*.yaml")
        sh("kubectl --namespace=production apply -f k8s/services/")
        sh("kubectl --namespace=production apply -f k8s/production/")
        sh("echo http://`kubectl --namespace=production get service/${feSvcName} --output=json | jq -r '.status.loadBalancer.ingress[0].ip'` > ${feSvcName}")
        break

    // Roll out a dev environment
    default:
        // Create namespace if it doesn't exist
        sh("kubectl get ns ${env.BRANCH_NAME} || kubectl create ns ${env.BRANCH_NAME}")
        // Don't use public load balancing for development branches
        sh("sed -i.bak 's#LoadBalancer#ClusterIP#' ./k8s/services/frontend.yaml")
        sh("sed -i.bak 's#eu.gcr.io/cloud-solutions-images/gceme:1.0.0#${imageTag}#' ./k8s/dev/*.yaml")
        sh("kubectl --namespace=${env.BRANCH_NAME} apply -f k8s/services/")
        sh("kubectl --namespace=${env.BRANCH_NAME} apply -f k8s/dev/")
        echo 'To access your environment run `kubectl proxy`'
        echo "Then access your service via http://localhost:8001/api/v1/proxy/namespaces/${env.BRANCH_NAME}/services/${feSvcName}:80/"
  }
}

I always get an error docker not found:

[apiservice_master-GJCRJX6ZJPDVVSEUHIS6VBX7OYMFS5WKRVRKCSF4PSO76ZGZPKFQ] Running shell script
+ docker build -t eu.gcr.io/xxxxx/apiservice:master.1 .
/var/jenkins_home/workspace/apiservice_master-GJCRJX6ZJPDVVSEUHIS6VBX7OYMFS5WKRVRKCSF4PSO76ZGZPKFQ@tmp/durable-b4503ecc/script.sh: 2: /var/jenkins_home/workspace/apiservice_master-GJCRJX6ZJPDVVSEUHIS6VBX7OYMFS5WKRVRKCSF4PSO76ZGZPKFQ@tmp/durable-b4503ecc/script.sh: docker: not found

What do I have to change to make docker work inside jenkins?

StephenKing
  • 36,187
  • 11
  • 83
  • 112
Tino
  • 3,340
  • 5
  • 44
  • 74
  • Did you see [Container builder](https://cloud.google.com/container-builder/docs/overview) on GCP? It's very simple to use, just `gcloud container builds submit . -t ${imageTag}` instead of `docker build` and it will also be automatically pushed. However, this doesn't solve your tests (perhaps some small test GKE env?). – Robert Lacok May 15 '17 at 10:06

2 Answers2

1

That looks like DiD (Docker in Docker), which this recent issue points out as problematic.
See "Using Docker-in-Docker for your CI or testing environment? Think twice."

That same issue recommends to run in privilege mode.
And make sure your docker container in which you are executing does have docker installed.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Thanks for the article. It helped to clear up a lot! You don't happen to have a tutorial or something where I can read up a complete solution that works? – Tino May 14 '17 at 12:47
  • @Tino Not exactly. I have other examples, but they might not run Jenkins itself in a Docker container: http://stackoverflow.com/q/42453967/6309, https://issues.jenkins-ci.org/browse/JENKINS-34276 (about using inside: https://go.cloudbees.com/docs/cloudbees-documentation/cje-user-guide/index.html#docker-workflow ) – VonC May 14 '17 at 14:43
  • @Tino Also https://github.com/kranthiB/Build-Continuous-Integration-with-Jenkins-and-Dokcer mentioned DiD as well as DooD (Docker outside of Docker) – VonC May 14 '17 at 14:43
  • https://www.reddit.com/r/docker/comments/50gtf9/how_to_build_docker_images_automatically_with/ does mention to map the docker socket into the jenkins container. – VonC May 14 '17 at 14:51
0

You need the docker client installed in the Jenkins agent image used for that node, ie. cloudbees/java-with-docker-client And the docker socket mounted in the agent

csanchez
  • 1,623
  • 11
  • 20