Search code examples
javadockercommand-line-interfacejib

Deploying Command Line Interface app using jib


Ok, I am building a jib based docker image which contains a java CLI app. The application is run like this :

java -jar app.jar --opt1=<opt1-value>

what i want to do is run a docker container which does nothing when the container is started but can accept arguments anytime and pass that arguments to the JVM application in the container and let it do its job. It looks like that when the jib docker container is run, the application is run and it closes itself.

Not sure how to go about doing this. Any help on this is much appreciated.


Solution

  • I think you're trying to use a container in an unconventional way, but I'll leave an answer anyway.


    I'll assume you run your container image on a Docker runtime.

    • Run java -jar app.jar inside a container.

      $ docker run --entrypoint java <your image> -jar app.jar
      
    • Run java -jar app.jar --opt1=some-value inside a container.

      $ docker run --entrypoint java <your image> -jar app.jar --opt1=some-value
      
      
    • Run sh inside a container, and use the shell.

      (This requires having the sh program installed in your image. An old version of Jib by default used a base image that doesn't have a shell program, so in that case, you would have needed to specify a different base image such as openjdk using the jib.from.image config option.)

      $ docker run -it --entrypoint sh <your image>
      (now you're in a "sh" process running inside a container)
      # ls
      ...
      # java -jar app.jar
      ...
      # exit
      

    Update based on the OP's comment:

    There are many ways to achieve what you want, and here's one:

    1. Run a container with a process that can hang while does nothing, such as the very common Linux tool sleep. (Again, as with sh above, you may use a base image that comes with the sleep binary, such as openjdk.)
      $ docker run --name my-running-container --entrypoint sleep <your image> infinity
      
      In another terminal, you'll be able to verify that your container named "my-running-container" is running with the command sleep infinity:
      $ docker ps                            
      CONTAINER ID   IMAGE           COMMAND            CREATED         STATUS         PORTS     NAMES         
      51c4568d2d2a   debian:buster   "sleep infinity"   6 seconds ago   Up 3 seconds             my-running-container
      
    2. When you want to run your app inside the running container, do
      $ docker exec my-running-container java -jar app.jar --opt1=some-value