简体   繁体   English

如何配置Jenkinsfile来构建docker映像并将其推送到私有注册表

[英]How to configure a Jenkinsfile to build docker image and push it to a private registry

I have two questions. 我有两个问题。 But maybe I don't know how to add tags for this question so that I added the tag for both. 但是也许我不知道如何为这个问题添加标签,所以我为这两个标签都添加了标签。 First question is related to Jenkins plugin usage to bake and push docker image using this . 第一个问题是有关詹金斯插件使用烘烤和使用推泊坞窗图像 Below is my Jenkinsfile script. 以下是我的Jenkinsfile脚本。 I finished building jar file in target directory. 我完成了在目标目录中构建jar文件的操作。 Then I want to run this docker plugin to bake with this artifact. 然后我要运行此docker插件以使用此工件进行烘焙。 As you know, we needed to have a Dockerfile so I put it in a root directory where git cloned the source. 如您所知,我们需要有一个Dockerfile,因此我将其放在git克隆源代码的根目录中。 How I configure this? 我该如何配置? I don't know how to this. 我不知道该怎么做。 If I run below, Jenkins told that there is no steps. 如果我在下面跑,詹金斯告诉我没有任何步骤。

pipeline {
    agent any
    stages {
        stage('build') {
            steps {
                git branch: 'master', credentialsId: 'e.joe-gitlab', url: 'http://70.121.224.108/gitlab/cicd/spring-petclinic.git'
                sh 'mvn clean package'
            }
        }
        stage('verify') {
            steps {
                sh 'ls -alF target'
            }
        }

        stage('build-docker-image') {
                steps {
                    docker.withRegistry('https://sds.redii.net/', 'redii-e.joe') {
                        def app = docker.build("sds.redii.net/e-joe/spring-pet-clinic-demo:v1",'.')
                        app.push()
                    }
                }
            }




    }
}

UPDATE this is another Jenkins Pipeline Syntax sniffet generator. 更新,这是另一个Jenkins管道语法嗅探器生成器。 But it doesn't work neither. 但这也不起作用。

pipeline {
    agent any
    stages {
        stage('build') {
            steps {
                git branch: 'master', credentialsId: 'e.joe-gitlab', url: 'http://70.121.224.108/gitlab/cicd/spring-petclinic.git'
                sh 'mvn clean package'
            }
        }
        stage('verify') {
            steps {
                sh 'ls -alF target'
            }
        }        
        stage('docker') {
                withDockerRegistry([credentialsId: 'redii-e.joe', url: 'https://sds.redii.net']) {
                    def app = docker.build("sds.redii.net/e-joe/spring-pet-clinic-demo:v1",'.')
                    app.push()
                }
        }
    }
}

Dokerfile is like the below. Dokerfile如下所示。 If I try baking image in my local, I got the following error. 如果我尝试在本地烘烤图像,则会出现以下错误。

container_linux.go:247: starting container process caused "chdir to cwd (\"/usr/myapp\") set in config.json failed: not a directory"
oci runtime error: container_linux.go:247: starting container process caused "chdir to cwd (\"/usr/myapp\") set in config.json failed: not a directory"
: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

DockerFile Docker文件

FROM openjdk:7
COPY ./target/spring-petclinic-1.5.1.jar /usr/myapp
WORKDIR /usr/myapp
RUN     java spring-petclinic-1.5.1.jar 

You are writing your .jar to /usr/myapp . 您正在将.jar写入/usr/myapp Which means that /usr/myapp will be the jar file and not a directory, resulting in that error. 这意味着/usr/myapp将是jar文件而不是目录,从而导致该错误。 Change your docker copy line to COPY ./target/spring-petclinic-1.5.1.jar /usr/myapp/ (with the trailing slash) and your Dockerfile should work. 将您的COPY ./target/spring-petclinic-1.5.1.jar /usr/myapp/ 复制行更改为COPY ./target/spring-petclinic-1.5.1.jar /usr/myapp/ (带有斜杠),您的Dockerfile应该可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM