我正在使用Jenkins kubernetes-plugin。是否可以从Dockerfile构建Docker映像,然后在创建的映像中运行步骤?该插件需要在Pod模板中指定一个图像,所以我的第一个尝试是使用docker-in-docker,但是步骤docker.image('jenkins/jnlp-slave').inside() {..}
失败了:
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"
}
}
}
}
}
}
失败:
WorkflowScript: 31: Expected a symbol @ line 31, column 11.
docker.image('jenkins/jnlp-slave').inside() {
答案 0 :(得分:2)
正如马特(Matt)在评论中指出的那样,该方法有效:
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"
}
}
}
}
}
}
}
答案 1 :(得分:0)
container('docker') {
script {
def image = docker.build("cusdock","CustomImage")
image.inside(){
sh "docker info"
}
}
}
这里,CustomImage是git repo内部的目录,其中包含Dockerfile,它将构建您的自定义映像。 该图像将被构建并标记为cusdock docker信息将在您的自定义docker映像中运行。