Problem with Dockerfile and jenkins

0

I'm starting with the dockerfile and jenkinsfiles and I'm pretty lost. I do not know exactly what the problem is. I'm trying to create smaller images.

This is my Jenkinsfile

node {
   // -- Configura variables
   echo 'Configurando variables'
   def mvnHome = tool 'M3'
   env.PATH = "${mvnHome}/bin:${env.PATH}"
   echo "var mvnHome='${mvnHome}'"
   echo "var env.PATH='${env.PATH}'"

   // -- Descarga código desde SCM
   echo 'Descargando código de SCM'
   sh 'rm -rf *'
   checkout scm
   stage('Package') {
       dir('PruebaDockerSVN') {
            sh 'mvn clean package -DskipTests'
       }
   }

   stage('Create Docker Image') {
       dir('PruebaDockerSVN') {
           docker.build("prueba-docker-svn")
       }
   }

   stage ('Run Application') {
       dir('PruebaDockerSVN') {
           try { 
               sh "docker run -it --rm --volume 
               "$PWD"/pom.xml://usr/src/app/pom.xml 
               \ --volume "$HOME"/.m2:/root/.m2 maven:3-jdk-8-alpine mvn 
               install"


           } catch (error) {
           } finally {}
        }
   }
}

My Dockerfile is this

FROM openjdk:8-jre-alpine
# ----
# Install Maven
RUN apk add --no-cache curl tar bash
ARG MAVEN_VERSION=3.3.9
ARG USER_HOME_DIR="/root"
RUN mkdir -p /usr/share/maven && \
curl -fsSL http://apache.osuosl.org/maven/maven- 
3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz | tar - 
xzC 
/usr/share/maven --strip-components=1 && \
ln -s /usr/share/maven/bin/mvn /usr/bin/mvn
ENV MAVEN_HOME /usr/share/maven
ENV MAVEN_CONFIG "$USER_HOME_DIR/.m2"
# speed up Maven JVM a bit
ENV MAVEN_OPTS="-XX:+TieredCompilation -XX:TieredStopAtLevel=1"
ENTRYPOINT ["/usr/bin/mvn"]

WORKDIR /app

# Add POM and source
ADD pom.xml /app/pom.xml
ADD src /app/src

# Build the app
RUN ["mvn", "clean", "package"]

# Run the app
RUN bash -c 'touch /app/target/pruebasvn-0.0.1-SNAPSHOT.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","- 
jar","/app/target/pruebasvn-0.0.1-SNAPSHOT.jar"]

When executing my Job in jenkins this is the error that gives me

[Pipeline] { (Create Docker Image)
[Pipeline] dir
Running in /var/lib/jenkins/workspace/prueba-docker-svn/PruebaDockerSVN
[Pipeline] {
[Pipeline] sh
[PruebaDockerSVN] Running shell script
+ docker build -t prueba-docker-svn .
Sending build context to Docker daemon 19.44 MB

Error response from daemon: Unknown instruction: /USR/SHARE/MAVEN
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 1
Finished: FAILURE

EDITED : this was resolved as @ César said.

Now my problem is different. I had to change the Dockerfile to take the jdk instead of the jre because it gave me errors, in the pom I had to put this, because I ran some tests and gave an error.

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <testFailureIgnore>true</testFailureIgnore>
    </configuration>
</plugin>

Keep giving error but at least it continues ... Now my only problem is that I create the image but it does not create the container. If I put in the console docker run -d -p 8585: 8080 test-docker-svn the application works perfectly.

so in the Jenkinsfile just below     sh "docker run -it --rm --volume                "$ PWD" /pom.xml://usr/src/app/pom.xml \     --volume "$ HOME" / .m2: /root/.m2 maven: 3-jdk-8-alpine mvn install "

I have put:     sh "docker run -d -p 8585: 8080 test-docker-svn"

but it is not doing it. I do not know if the problem comes from this jenkins outing when it ends

[Pipeline] { (Run Application)
[Pipeline] dir
Running in /var/lib/jenkins/workspace/prueba-docker-svn/PruebaDockerSVN
[Pipeline] {
[Pipeline] sh
[PruebaDockerSVN] Running shell script
+ docker run -it --rm --volume
flag needs an argument: --volume
See 'docker run --help'.
[Pipeline] }
[Pipeline] // dir
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS
    
asked by l.peXi 26.06.2018 в 14:14
source

1 answer

1

It seems that you have line breaks in your Dockerfile, specifically in the part of the first RUN after the tar command. It should be:

FROM openjdk:8-jre-alpine

# ...

RUN mkdir -p /usr/share/maven && \
  curl -fsSL http://apache.osuosl.org/maven/maven-3/$MAVEN_VERSION/binaries/apache-maven-$MAVEN_VERSION-bin.tar.gz | tar - xzC /usr/share/maven --strip-components=1 && \
  ln -s /usr/share/maven/bin/mvn /usr/bin/mvn

# ...
    
answered by 26.06.2018 в 18:10