通过Jenkins SCM(Jenkinsfile和代码一起回购)构建和运行Docker镜像,Jenkins本身使用dockerfile在docker中运行

时间:2019-07-08 11:25:46

标签: spring-boot docker jenkins jenkins-pipeline dockerfile

我正在尝试通过Jenkins为Spring引导应用程序构建动态容器系统。为此,我通过docker运行了Jenkins,并设置了一个项目,该项目将从我的存储库中获取代码并读取jenkins文件。

该项目同时具有Dockerfile和Jenkins文件,因此在读取之后,jenkins将读取jenkinsfile,该文件随后将读取dockerfile并生成并运行,或者流程应该如此。我的应用程序的构建已创建,但是在docker image的运行部分遇到了问题。

我通过以下命令运行了詹金斯:

docker run -d -v jenkins_home:/var/jenkins_home -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker --name jenkinsHome  -p 8089:8080 -p 50000:50000 jenkins/jenkins:lts

这是我制作的dockerfile内容

# Start with a base image containing Java runtime
FROM gradle:5.0.0-jdk8-alpine as builder
COPY --chown=gradle:gradle . /home/gradle/src
WORKDIR /home/gradle/src
RUN gradle build


# Add Maintainer Info
LABEL maintainer="teckacademy@gmail.com"

# Add a volume pointing to /tmp
VOLUME /tmp

# Make port 8080 available to the world outside this container
FROM openjdk:8-jdk-alpine
EXPOSE 8081
COPY --from=builder /home/gradle/src/build/libs/*.jar /app/
#COPY --from=builder /home/gradle/src/build/libs/*.jar /app/app.jar
RUN ls -r /app
#RUN mkdir -p /app
# The application's jar file
#ARG JAR_FILE=/app/build/libs/aggregator-0.1-SNAPSHOT.jar
ARG PROPERTIES=src/main/resources/application.properties
#ARG PROPERTIES_DEV=src/main/resources/application-dev.properties
ARG PROPERTIES_DEV_YML=src/main/resources/application-qa.yml
ARG PROPERTIES_YML=src/main/resources/application.yml
ARG LOG=logback.xml

# Add the application's jar to the container
#ADD ${JAR_FILE} /app/app.jar
ADD ${PROPERTIES} /app/application.properties
ADD ${PROPERTIES_YML} /app/application.yml
ADD ${PROPERTIES_DEV_YML} /app/application-qa.yml
ADD ${LOG} /app/logback.xml

#RUN apk update
#RUN apk add --update mysql mysql-client && rm -f /var/cache/apk/*
#RUN apk add libmysql-java

#RUN bash -c 'touch /app.jar'
# Run the jar file
#ENTRYPOINT ["java","-    Dspring.config.location=./application.properties", "-jar","/app.jar"]
ENTRYPOINT ["java" ,"-Djava.security.egd=file:/dev/./urandom -    Dspring.profile.active=dev","-jar","/app/reppy-aggregator-dev-0.1-    SNAPSHOT.jar"]
#RUN sh docker stop aggregator-dev
#RUN sh docker build -t aggregator-dev .
#RUN sh docker run -d -p 8081:8081 --name aggregator-dev aggregator-    dev
#RUN sh 'java -jar -Dspring.profile.active=dev /app/app.jar &'
#ENV JAVA_OPTS=""
#ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -        Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]

请忽略散列,我保留它们以显示在此流程中我还尝试过的其他事情。

我拥有的詹金斯文件具有以下代码:

pipeline {
agent {
    dockerfile true
    // args '-d -p 8081:8081'
}
stages {
    stage("Checkout") {
        steps {
            // git url: 'https://github.com/mrityush/reppy-aggregator.git', credentialsId: 'tecklabs', branch: 'qa'
            git url: 'https://github.com/mrityush/reppy-aggregator.git', credentialsId: '65b349b7-b6eb-4073-b4d1-194912ccc5b3', branch: 'qa'
        }
    }
    stage('Build image') {
        steps {
            script{
                docker.build aggregator-dev
            }
        }
    }
}
}

我需要通过jenkinsfile和dockerfile对以下2条命令做出等效声明:

docker build -t aggregator-dev .
docker run -d -p 8081:8081 aggregator-dev

但是我不确定如何在两个文件中的任何一个中做

无论我尝试哪种组合,以下错误都是我在詹金斯构建期间始终收到的错误。

Step 19/19 : ENTRYPOINT ["java" ,"-    Djava.security.egd=file:/dev/./urandom -Dspring.profile.active=dev","-    jar","/app/reppy-aggregator-dev-0.1-SNAPSHOT.jar"]
 ---> Running in df4ac65aad14
Removing intermediate container df4ac65aad14
 ---> 397efd233dff
Successfully built 397efd233dff
Successfully tagged     fe5ea528d5e52cbde8d07a84eb2f351d11de9c94:latest
[Pipeline] dockerFingerprintFrom
[Pipeline] }
[Pipeline] // stage
[Pipeline] sh
+ docker inspect -f . fe5ea528d5e52cbde8d07a84eb2f351d11de9c94
.
[Pipeline] withDockerContainer
Jenkins seems to be running inside container     b278ec9f6f776a9371dc8772e9ca2e1effe89ccba51601c95bd22b5819502074
$ docker run -t -d -u 1000:1000 -w     /var/jenkins_home/workspace/reppy-aggregator-dev --volumes-from     b278ec9f6f776a9371dc8772e9ca2e1effe89ccba51601c95bd22b5819502074 -e     ******** -e ******** -e ******** -e ******** -e ******** -e ******** -    e ******** -e ******** -e ******** -e ******** -e ******** -e ********     -e ******** -e ******** -e ******** -e ******** -e ******** -e     ******** -e ******** -e ******** -e ******** -e ******** -e ******** -    e ******** -e ******** -e ********     fe5ea528d5e52cbde8d07a84eb2f351d11de9c94 cat
$ docker top     fdad8c11f9d1a627bacffd90eded3e0e87a1c32612a9edaeb3c8b6237cae83ba -eo     pid,comm
ERROR: The container started but didn't run the expected command.     Please double check your ENTRYPOINT does execute the command passed as     docker run argument, as required by official docker images (see     https://github.com/docker-library/official-images#consistency for     entrypoint consistency requirements).
Alternatively you can force image entrypoint to be disabled by     adding option `--entrypoint=''`.

请让我知道如何解决此docker build run(通过容器中的jenkins)问题。谢谢。

1 个答案:

答案 0 :(得分:0)

使用Dockerfile作为代理时,将自动为您构建映像。您要做的就是在该步骤中执行所需的命令。

pipeline {
 agent {
  dockerfile true
 }
 stages {
  stage('Use image') {
    steps {
        script{
            sh "uname -a"
        }
    }
  }
 }
}

以上管道构建了映像并为我提供了操作系统详细信息。存储库链接在https://github.com/rkamath3/ubuntu.git