jpganz18
jpganz18

Reputation: 5878

docker with a java -jar but with arguments?

I am wondering if is possible to do the following...

I have a small application in java (and gradle)

I am trying to set it in a container, so I created the Dockerfile and worked fine UNTIL I have to set a parameter.

To run it locally on my IDLE I set these program arguments:

server application-local.yml

That actually loads a file with many properties, without this the app will fail.

On my Dockerfile I have this

FROM openjdk:8-jdk-alpine

USER root:root
ENV NAME test

LABEL maintainer="myself"

WORKDIR /opt/apps

COPY build/libs/myapp.jar /opt/apps/myapp.jar

EXPOSE 8080

CMD ["java", "myapp.jar"]

I was wondering if I could do the following:

CMD ["java", "-server:application-local.yml", "myapp.jar"]

but doesnt work, also I cannot do

java -jar myapp.jar -server:application-local.yml 

simply doesnt do anything Dont wanna do a gradleBuild because it is supposed to use java only at my docker image...

Any idea how to do this?

Edit:

So, I have done the following, move my application-local.yml to a folder I can copy and added this

COPY some-path/application-local.yml /opt/apps/local.yml

and moved the CMD for this

CMD ["java", "myapp.jar", "server", "local.yml"]

I still get the same error, basically cannot resolve the values from that yml file.

EDIT 2 ---

Basically what I cannot do is to figure out how to send the application.default.yml as configuration file, I realise also that the -server does not anything, is my config file the one I cannot load and is not present in the jar (normal behaviour)

Upvotes: 5

Views: 6718

Answers (2)

nackjicholson
nackjicholson

Reputation: 4839

This was so annoying to figure out, but its the only way I could get anything to work with something like $JAVA_OPTS also being something that can be defined as environmental config.

Dockerfile, needs ENTRYPOINT to be specified in this "list" style.

COPY /target/mycli-standalone.jar /opt/mycorp/mycli-standalone.jar
COPY ./devops/mycli /opt/mycorp/
ENTRYPOINT ["/opt/mycorp/mycli"]

The script at ./devops/mycli needs to accept all arguments and forward them along.

#!/bin/sh
java $JAVA_OPTS -jar /opt/mycorp/mycli-standalone.jar ${@}

Upvotes: 0

Ferrybig
Ferrybig

Reputation: 18834

When using the java command, order of the arguments matter

First you put the java options, like -server, then the -jar ... Or the class name in the case of running classes directly, and then the application arguments

The proper way to run your app is:

CMD ["java", "-jar", "myapp.jar", "server", "application-local.yml"]

Then you have another problem, you are only copying the final jar file to your docker container, so it can can't find the application-local.yml file, copy this also to your docker container

COPY build/libs/application-local.yml /opt/apps/application-local.yml

Upvotes: 1

Related Questions