1

I have an executable JAR that is normally executed like so:

java -jar myapp.jar 

I would like to "Dockerize" this app by placing it in a container, say, under /~/myapp, and then configure the container to always run this app (using the above command) when the container starts up.

Using this sample Dockerfile as a starting point, what entries do I need to add in order to get Docker to position myapp.jar correctly in the file system, and to run it at startup?

Assume that by the time I run Docker to build the image, the binary will be located under build/distributions like so:

myapp/ src/ build.gradle Dockerfile build/distributions/ myapp.jar 
4
  • 2
    See ADD and COPY to copy files from your host to your container, docs.docker.com/reference/builder/#add and docs.docker.com/reference/builder/#copy Commented Sep 2, 2015 at 11:41
  • Thanks @user2915097 (+1) - two quick followups if you don't mind: (1) do either of these instructions create directories for you if they don't previously exist? Their docs don't say either way. And (2) can I then assume that I'd want to use a RUN or CMD instruction to run the executable jar? Thanks again! Commented Sep 2, 2015 at 11:58
  • in order to run the executable jar, you will use either CMD or ENTRYPOINT, see for example stackoverflow.com/questions/30752853/… Commented Sep 2, 2015 at 12:00
  • extract from docs.docker.com/reference/builder/#add "All new files and directories are created with a UID and GID of 0." Commented Sep 2, 2015 at 12:02

1 Answer 1

2

Here is a simple example of what you are requesting. This assumes you are running docker build . from /myapp/src/ and that the application is running in the foreground of the container.

# Use Ububtu 14.04 as our base O/S FROM ubuntu:14.04 # Set our working directory WORKDIR / # Update the repositories and then install java RUN apt-get update && install -y default-jre # Copy the application from its folder to our image # Assumes docker build is run from /myapp/src ADD /build/distributions/myapp.jar /myapp.jar # Run the app when the container is executed. CMD ["java", "-jar myapp.jar"] 
Sign up to request clarification or add additional context in comments.

6 Comments

Ahhh thank you @GHETTO.CHILD (+1) - a quick followup if you don't mind: when you say "assumes...that the application is running in the foreground", can you elaborate? What line of code above makes this assumption? What are the implications of running in the foreground? What happens when you run the app in the background? Thanks again!
Most docker images have the main application/service running in the foreground for logging purposes. This does assume you've redirected logging to standard out and error.
Thanks @booyaa (+1) - so can I assume then that running the app in the background means you can't log to STDOUT/STDERR?
When @booyaa says "in the foreground" s/he means in the foreground of the Docker image's process. The output of the program run in CMD will be available through the "docker logs" command.
@AdrianMitev I don't disagree there, again, this is just an example to give an primer on how to do it.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.