3

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?

scottysseus
  • 1,922
  • 3
  • 25
  • 50

2 Answers2

2

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"]
ErikMD
  • 13,377
  • 3
  • 35
  • 71
0

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.

Michael Miller
  • 399
  • 1
  • 9
  • If you're not getting the outcome you want, you may have to glance at the entrypoint script to ensure that you understand how it kicks off the service. – Michael Miller Feb 11 '19 at 18:11