title: Build And Run Docker Image through Jenkins SCM (Jenkinsfile pushed along with code to repo), Jenkins itself running in docker, using dockerfile
body: <p>I am trying to build a dynamic container system for my Spring boot application through Jenkins. For that, I ran Jenkins through docker, and set a project that would fetch code from my repository and read the jenkins file. </p>
  
  <p>The project has Dockerfile as well as Jenkins file, so that after reading, jenkins would read jenkinsfile which would then read dockerfile and build and run, or so should the flow be. The build for my application gets created, however I am facing issue in the run part of docker image.</p>
  
  <p>I ran jenkins through following command:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>This is the dockerfile content I made</p>
  
  <pre><code># 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 &amp;&amp; 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 &amp;'
  #ENV JAVA_OPTS=""
  #ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -        Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
  </code></pre>
  
  <p>please ignore the hashed lines, I kept them to show what else i tried in this flow.</p>
  
  <p>The jenkins file I have has following code:</p>
  
  <pre><code>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
              }
          }
      }
  }
  }
  </code></pre>
  
  <p>I need to make quivalent statements of following 2 commands through jenkinsfile and dockerfile:</p>
  
  <pre><code>docker build -t aggregator-dev .
  docker run -d -p 8081:8081 aggregator-dev
  </code></pre>
  
  <p>but I am not sure how to do it in any of the two files</p>
  
  <p>The following error is what I receive consistently during jenkins build, no matter which combination I try. </p>
  
  <pre><code>Step 19/19 : ENTRYPOINT ["java" ,"-    Djava.security.egd=file:/dev/./urandom -Dspring.profile.active=dev","-    jar","/app/reppy-aggregator-dev-0.1-SNAPSHOT.jar"]
   ---&gt; Running in df4ac65aad14
  Removing intermediate container df4ac65aad14
   ---&gt; 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=''`.
  </code></pre>
  
  <p>Kindly let me know how this docker build run (through jenkins in a     container) issue can be solved. Thanks.</p>