docker run pass arguments to entrypoint

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
108548 次浏览

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

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