[英]Jenkins Kubernetes plugin: How to build image from Dockerfile and run steps inside the image
我正在使用 Jenkins kubernetes-plugin 。 是否可以从 Dockerfile 构建 docker 镜像,然后在创建的镜像中运行步骤? 该插件需要在 pod 模板中指定一个图像,所以我的第一次尝试是使用docker.image('jenkins/jnlp-slave').inside() {..}
-in- docker.image('jenkins/jnlp-slave').inside() {..}
但步骤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() {
正如马特在评论中指出的那样,这是有效的:
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"
}
}
}
}
}
}
}
container('docker') {
script {
def image = docker.build("cusdock","CustomImage")
image.inside(){
sh "docker info"
}
}
}
这里的 CustomImage 是你的 git repo 中的目录,它包含将构建你的自定义镜像的 Dockerfile。 该图像将被构建并标记为 cusdock docker info 将在您的自定义 docker 图像中运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.