5

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)

7
  • why -server:appl... (minus and colon) and not just server appl... (space inbetween arguments, or even separated strings)? Never tried it, but above is IMO inconsistent: arguments server application-local.yml does not match in any way -server:application.yml Commented Dec 13, 2018 at 8:06
  • I dont understand/see your point :| Commented Dec 13, 2018 at 8:16
  • Before loading your app onto the container, you need to figure out how to package and start it from the command line. Your question suggests that you use your IDE to run the app. The IDE configures two things I don't see in your command-line invocation: the name of the class containing the main method and the classpath. Create a jar from your IDE and try this in the command-line: java -cp <jar-path> <main-class> server application-local.yml If that works, you should use that command in your docker image. Commented Dec 13, 2018 at 8:25
  • the CMD will be looking for the file application-local.yml from within the container (and not from host)... I don't see you copying it, here. Are you running your application through a compose file (and copying it there) or is this actually missing? Commented Dec 13, 2018 at 8:26
  • the application is already packaged in a jar, I am now copying the config to the main path and send it as parameter in other way (but now I am getting another error) Error: Unable to access jarfile.... Commented Dec 13, 2018 at 8:45

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

thanks, but I did that already (its on my question) I also have to say taht the file is application-default.yml that does not seem to be reference anywhere (so I bet is default name for a gradle project? like the application.properties I guess)
0

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 ${@}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.