javadockerdockerfileapache-commons-cli

Dockerfile of console application that accept arguments in Commons CLI style


I have a java console application that I package as jar and run it as

java -jar target/myProject-1.0-SNAPSHOT.jar -arg1 145 -arg2 345 -arg3 99

I want to run the same command inside a container and pass these arguments (arg1, arg2, arg3) to docker run command. My docker file look like:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/myProject-1.0-SNAPSHOT.jar myProject-1.0-SNAPSHOT.jar
ENV JAVA_OPTS=""
ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /myProject-1.0-SNAPSHOT.jar" ]

then I try to run the image as follows:

docker run myProject:0.3 -e  -arg1 145 -arg2 345 -arg3 99

but my program don't get the arguments. what I'm missing ?


Solution

  • You have to add the ENV command in the DOCKERFILE so that you can receive the arguments that you are passing in and then pass that onto the ENTRYPOINT script

    Dockerfile will look something like this

    FROM openjdk:8-jdk-alpine
    VOLUME /tmp
    ENV arg1
    ENV arg2
    ENV arg3
    ADD target/myProject-1.0-SNAPSHOT.jar myProject-1.0-SNAPSHOT.jar
    ENV JAVA_OPTS=""
    ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar /myProject-1.0-SNAPSHOT.jar ${arg1} ${arg2} ${arg3}" ]
    

    Let me know if you have any questions