Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create docker image for local application taking file and value parameters

I have a java application (jar file) that I want to be able to run from a docker image.

I have created a Dockerfile to create an image using centos as base and install java as such:

Dockerfile
FROM centos
RUN yum install -y java-1.7.0-openjdk

I ran docker build -t me/java7 after to obtain the image me/java7

however I am stuck at some dead ends.

  1. How do I copy the jar file from the host into the image/container
  2. I require 2 parameters. 1 is a file, which needs to be copied into a directory into the container at runtime. The other is a number which needs to be passed to the jar file in the java -jar command automatically when the user runs docker run with the parameters

Extra Notes:

The jar file is a local file. Not hosted anywhere accessible via wget or anything. The closest I have at the moment is a windows share containing it. I could also access the source from a git repository but that would involve compiling everything and installing maven and git on the image so I'd rather avoid that.

any help is much appreciated.

like image 531
mangusbrother Avatar asked Aug 18 '14 13:08

mangusbrother


People also ask

Which is the docker command to build a docker image using a docker file in the current directory?

With Dockerfile written, you can build the image using the following command: $ docker build .

Which file is used for creating docker image?

The docker build command builds Docker images from a Dockerfile and a “context”. A build's context is the set of files located in the specified PATH or URL . The build process can refer to any of the files in the context.


Video Answer


2 Answers

Suppose your file structure is as follow :

DockerTest
        └── Dockerfile
        └── local.jar

Dockerfile content will be :

FROM centos
RUN yum install -y java-1.7.0-openjdk
EXPOSE 8080
ADD /local.jar fatJar.jar
ENTRYPOINT ["java","-jar","fatJar.jar"]

Use following command :

$ cd DockerTest
$ docker build -f Dockerfile -t demo .
like image 135
Riddhi Gohil Avatar answered Oct 16 '22 16:10

Riddhi Gohil


  1. In the Dockerfile, add a local file using ADD, e g

    ADD your-local.jar /some-container-location
    
  2. You could use volumes to put a file in the container in runtime, e g

    VOLUME /copy-into-this-dir
    

    And then you run using

    docker run -v=/location/of/file/locally:/copy-into-this-dir -t me/java7
    

    You can use ENTRYPOINT and CMD to pass arguments, e g

    ENTRYPOINT ["java", "-jar", "/whatever/your.jar"]
    CMD [""]
    

    And then again run using

    docker run -v=/location/of/file/locally:/copy-into-this-dir -t me/java7 --myNumber 42
    

(Have a look at the Dockerfile documentation.)

like image 26
frvi Avatar answered Oct 16 '22 17:10

frvi