Home > Software design >  How to concat arguments to an existing Dockerfile CMD?
How to concat arguments to an existing Dockerfile CMD?

Time:10-10

Suppose we have a Dockerfile with some CMD and we produced and image from it. Now suppose that we write a docker-compose and one of the services is built from that image.

What I want to do is running the same command but concatenating my own new parameters.

As an example, suppose the original CMD is

java -jar app.jar --argumentA=valA

I want the command to be

java -jar app.jar --argumentA=valA --argumentB=valB

Is it possible?

CodePudding user response:

I'm not entirely sure if this is what you would want to accomplish, but...

Dockerfile exposes both ENTRYPOINT and CMD for being able to execute commands. These also can be used in conjunction, but in this case the ENTRYPOINT will be the command what we want to execute and the CMD will represent some default arguments for the ENTRYPOINT (docs).

For example:

FROM openjdk:11

COPY . /target/app.jar

ENTRYPOINT ["java", "-jar", "app.jar", "--argumentA=valA"]

CMD ["--argumentB=valB"]

The --argumentB=valB will be appended to the java -jar app.jar --argumentA=valA, if we run the image like this:

docker build -t app .
docker run app # # the command executed will be java -jar app.jar --argumentA=valA --argumentB=valB

But the CMD part will be overridden if we provide other arguments when we run the docker image:

docker build -t app .
docker run app --argumentA=valC # the command executed will be java -jar app.jar --argumentA=valA --argumentB=valC

Also, we can commit the CMD and have the ENTRYPOINT only, if we don't require some defaults to be appended to the ENTRYPOINT.

  • Related