scottysseus
scottysseus

Reputation: 2020

How do I specify run arguments for a base image in a Dockerfile?

I am defining an image in a Dockerfile which has another image as its parent:

FROM parent_org/parent:1.0.0
...

The parent image's documentation mentions an argument (special-arg) that can be passed when running an instance of the container:

docker run parent_org/parent:1.0.0 --special-arg

How can I enable special-arg in my Dockerfile?

Upvotes: 3

Views: 4391

Answers (2)

ErikMD
ErikMD

Reputation: 14743

TL;DR: you could use the CMD directive by doing something like this:

FROM parent_org/parent:1.0.0
CMD ["--special-arg"]

however note that passing extra flags to docker run as below would overwrite --special-arg (as CMD is intended to specify default arguments):

docker build -t child_org/child .
docker run child_org/child  # would imply --special-arg

docker run child_org/child --other-arg  # "--other-arg" replaces "--special-arg"

If this is not what you'd like to obtain, you should redefine the ENTRYPOINT as suggested below.

The CMD and ENTRYPOINT directives

To have more insight on CMD as well as on ENTRYPOINT, you can take a look at the table involved in this other SO answer: CMD doesn't run after ENTRYPOINT in Dockerfile.

In your case, you could redefine the ENTRYPOINT in your child image (and if need be, the default CMD) by adapting child_org/child/Dockerfile w.r.t. what was defined in the parent Dockerfile.

Assuming the parent_org/parent/Dockerfile looks like this:

FROM debian:stable  # for example

WORKDIR /usr/src/foo
COPY entrypoint.sh .
RUN chmod a+x entrypoint.sh

ENTRYPOINT ["./entrypoint.sh"]
CMD ["--default-arg"]

You could write a child_org/child/Dockerfile like this:

FROM parent_org/parent:1.0.0
RUN […]

# Redefine the ENTRYPOINT so the --special-arg flag is always passed
ENTRYPOINT ["./entrypoint.sh", "--special-arg"]

# If need be, redefine the list of default arguments,
# as setting ENTRYPOINT resets CMD to an empty value:
CMD ["--default-arg"]

Upvotes: 2

Michael Miller
Michael Miller

Reputation: 399

This baffled me at first, too... Run them using the command: declaration... A command and an Entrypoint are two different things... The entrypoint runs whatever script/execution call your service needs to initialize and start. That entrypoint script then usually runs logic to append whatever you pass in from the command: declaration as further arguments to alter the behavior of the service.

Upvotes: 0

Related Questions