Reputation: 5229
I am using the Jenkins kubernetes-plugin. 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 docker.image('jenkins/jnlp-slave').inside() {..}
fails:
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"
}
}
}
}
}
}
Fails with:
WorkflowScript: 31: Expected a symbol @ line 31, column 11.
docker.image('jenkins/jnlp-slave').inside() {
Upvotes: 4
Views: 4610
Reputation: 1405
container('docker') {
script {
def image = docker.build("cusdock","CustomImage")
image.inside(){
sh "docker info"
}
}
}
Here CustomImage is the directory inside your git repo which contains Dockerfile which will build your custom image. That image will be build and tagged as cusdock docker info will be run inside your custom docker image.
Upvotes: 0
Reputation: 5229
As pointed out by Matt in the comments this works:
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"
}
}
}
}
}
}
}
Upvotes: 5