Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 605
nad87563 Avatar asked Nov 29 '18 16:11

nad87563


People also ask

How do I pass args to ENTRYPOINT docker?

So if you want to pass the URL argument to ENTRYPOINT, you need to pass the URL alone. The reason is we have the ab command as part of the ENTRYPOINT definition. And the URL you pass in the run command will be appended to the ENTRYPOINT script. In this case, CMD instruction is not required in the Dockerfile.

How do you pass a docker argument?

If you want to pass multiple build arguments with docker build command you have to pass each argument with separate — build-arg. docker build -t <image-name>:<tag> --build-arg <key1>=<value1> --build-arg <key2>=<value2> .

Does docker run override ENTRYPOINT?

ENTRYPOINT is the other instruction used to configure how the container will run. Just like with CMD, you need to specify a command and parameters. However, in the case of ENTRYPOINT we cannot override the ENTRYPOINT instruction by adding command-line parameters to the `docker run` command.

Can we use CMD and ENTRYPOINT together?

Example of using CMD and ENTRYPOINT togetherIf both ENTRYPOINT and CMD are present, what is written in CMD is executed as an option of the command written in ENTRYPOINT. If an argument is added at the time of docker run , the contents of CMD will be overwritten and the ENTRYPOINT command will be executed.


Video Answer


2 Answers

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

like image 56
Siyu Avatar answered Oct 18 '22 20:10

Siyu


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

like image 57
grapes Avatar answered Oct 18 '22 20:10

grapes