<p>I am using the Jenkins <a href="https://github.com/jenkinsci/kubernetes-plugin" rel="nofollow noreferrer">kubernetes-plugin</a>. Is it possible to build a docker image from a Dockerfile and then run steps inside the created image? The plugin requires to specify an image in the pod template so my first try was to use docker-in-docker but the step <code>docker.image('jenkins/jnlp-slave').inside() {..}</code> fails:</p>

<pre><code>pipeline {
  agent {
    kubernetes {
      //cloud 'kubernetes'
      label 'mypod'
      yaml """
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: docker
    image: docker:1.11
    command: ['cat']
    tty: true
    volumeMounts:
    - name: dockersock
      mountPath: /var/run/docker.sock
  volumes:
  - name: dockersock
    hostPath:
      path: /var/run/docker.sock
"""
    }
  }
  stages {
    stage('Build Docker image') {
      steps {
        git 'https://github.com/jenkinsci/docker-jnlp-slave.git'
        container('docker') {
          sh "docker build -t jenkins/jnlp-slave ."
          docker.image('jenkins/jnlp-slave').inside() {
            sh "whoami"
          }
        }
      }
    }
  }
}
</code></pre>

<p>Fails with:</p>

<pre><code>WorkflowScript: 31: Expected a symbol @ line 31, column 11.
             docker.image('jenkins/jnlp-slave').inside() {
</code></pre>

<p>As pointed out by Matt in the comments this works:</p>

<pre><code>pipeline {
  agent {
    kubernetes {
      //cloud 'kubernetes'
      label 'mypod'
      yaml """
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: docker
    image: docker:1.11
    command: ['cat']
    tty: true
    volumeMounts:
    - name: dockersock
      mountPath: /var/run/docker.sock
  volumes:
  - name: dockersock
    hostPath:
      path: /var/run/docker.sock
"""
    }
  }
  stages {
    stage('Build Docker image') {
      steps {
        git 'https://github.com/jenkinsci/docker-jnlp-slave.git'
        container('docker') {
          script {
            def image = docker.build('jenkins/jnlp-slave')
            image.inside() {
              sh "whoami"
            }
          }
        }
      }
    }
  }
}
</code></pre>
