docker run pass arguments to entrypoint

DockerDockerfile

Docker Problem Overview


I am able to pass the environment variables using -e option. But i am not sure how to pass command line arguments to the jar in entrypoint using the docker run command.

Dockerfile

FROM openjdk
ADD . /dir
WORKDIR /dir
COPY ./test-1.0.1.jar /dir/test-1.0.1.jar
ENTRYPOINT java -jar /dir/test-1.0.1.jar

test.sh

#! /bin/bash -l

export AWS_ACCESS_KEY_ID=$(aws configure get aws_access_key_id)
export AWS_SECRET_ACCESS_KEY=$(aws configure get aws_secret_access_key)

$value=7

docker run -e AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY  -i -t testjava  $value

Docker Solutions


Solution 1 - Docker

Use ENTRYPOINT in its exec form

ENTRYPOINT ["java", "-jar", "/dir/test-1.0.1.jar"]

then when you run docker run -it testjava $value, $value will be "appended" after your entrypoint, just like java -jar /dir/test-1.0.1.jar $value

Solution 2 - Docker

You should unleash the power of combination of ENTRYPOINT and CMD.

Put the beginning part of your command line, which is not expected to change, into ENTRYPOINT and the tail, which should be configurable, into CMD. Then you can simple append necessary arguments to your docker run command. Like this:

Dockerfile

FROM openjdk
ADD . /dir
WORKDIR /dir
COPY ./test-1.0.1.jar /dir/test-1.0.1.jar
ENTRYPOINT ["java", "-jar"]
CMD ["/dir/test-1.0.1.jar"]

Sh

# this will run default jar - /dir/test-1.0.1.jar
docker run testjava

# this will run overriden jar
docker run testjava /dir/blahblah.jar

This article gives a good explanation: https://medium.freecodecamp.org/docker-entrypoint-cmd-dockerfile-best-practices-abc591c30e21

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionnad87563View Question on Stackoverflow
Solution 1 - DockerSiyuView Answer on Stackoverflow
Solution 2 - DockergrapesView Answer on Stackoverflow