0

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.

500865
  • 6,920
  • 7
  • 44
  • 87
  • A Docker container generally runs a single process. Could you build the Java application to stay running and accept input as you describe? Is there any benefit to running the application in a JVM in a container, over using a JVM without Docker involved? – David Maze Mar 02 '21 at 15:46
  • it is basically a command line app and not a web app that stays running. one benefit is the convenience. instead of making our app work on all platforms (it has some native libraries), we can just build it for linux and keep it simple. also, we find working with docker to be very simple, easy & unified. – 500865 Mar 02 '21 at 15:54
  • @DavidMaze : Please see the comment above. – 500865 Mar 02 '21 at 16:03

1 Answers1

1

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
    
Chanseok Oh
  • 3,920
  • 4
  • 23
  • 63
  • we don't want to start container whenever we want to run the java application. we want to keep the container running and we want to run the application in the running container whenever there is a need. primary concern is to keep the container running without running the java application. – 500865 Mar 07 '21 at 15:06
  • `sleep missing operand` error is being thrown. i get the idea though. nice thinking. – 500865 Mar 09 '21 at 07:12
  • You should pass `infinity` as an argument to `sleep`. Type the command exactly as I did. – Chanseok Oh Mar 09 '21 at 13:48
  • although, the suggestion (available in the update section) doesn't feel neat and feels hacky, it certainly solves the problem. so, accepting it as a solution. – 500865 Mar 15 '21 at 07:42