skip to Main Content

I have a Spring boot application in a Docker container and when I run the command to execute tests I see that the app starts correctly but there is no test executed. Looks like the mvn test is completely ignored.

Below my docker commands:

docker build -t cygnetops/react-test -f Dockerfile.dev .
docker  run  cygnetops/react-test mvn test

Dockerfile.dev

FROM eclipse-temurin:17-jdk-alpine
VOLUME /tmp
ADD /target/demoCI-CD-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

EXPOSE 5000

2

Answers


  1. Entrypoints and commands are working together in a Docker container, if you specify an entrypoint, your command will be passed as an argument to the entrypoint, and if that entrypoint does nothing with the arguments passed to it, then the behaviour you are observing it totally normal.

    The chapter "Understand how CMD and ENTRYPOINT interact" of the documentation provides a nice table explaining in depth how they interact.

    In order to run you tests from that image, you could override the entrypoint when running you container:

    docker run --entrypoint "" cygnetops/react-test mvn test
    

    Note:

    1. you will also have to install Maven, as it is not part of your base image
    2. as you pointed, you will also need the POM and files of the Java project in order to run the tests, so you need to copy those sources in the image

    So, add, in your Dockerfile, the lines:

    COPY . .
    
    RUN apk add --no-cache maven
    

    If you want both to work, on the other hand, you will have to write your own entrypoint and make something from the command passed as arguments.

    Here is an example:
    entrypoint.sh, should be located at the same level as your Dockerfile:

    #!/usr/bin/env sh
    
    exec "$@" # execute what is passed as argument
    
    java -Djava.security.egd=file:/dev/./urandom -jar /app.jar
    

    Then, for your Dockerfile

    FROM eclipse-temurin:17-jdk-alpine
    VOLUME /tmp
    
    RUN apk add --no-cache maven
    
    COPY . .
    COPY /target/demoCI-CD-0.0.1-SNAPSHOT.jar app.jar
    COPY entrypoint.sh /usr/local/bin/
    ENTRYPOINT ["entrypoint.sh"]
    
    EXPOSE 5000
    
    Login or Signup to reply.
  2. Maybe a better solution is to create a docker file that runs a script instead of plain java. for example create a runner.sh file as follow:

    #!/bin/bash
    CMD="java -jar app.jar"
    $CMD &
    SERVICE_PID=$!
    mvn test
    wait "$SERVICE_PID"
    

    and this will be your dockerfile

    FROM maven:3.9.0-eclipse-temurin-11-alpine
    COPY . .
    RUN mvn install
    COPY runner.sh /scripts/runner.sh
    RUN ["chmod", "+x", "/scripts/runner.sh"]
    ENTRYPOINT ["/scripts/runner.sh"]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search