Pass ARG to ENTRYPOINT

13,814

Solution 1

You could combine ARG and ENV in your Dockerfile, as I mention in "ARG or ENV, which one to use in this case?"

ARG FOO
ENV FOO=${FOO}

That way, you docker.r2g can access the ${FOO} environment variable.

I guess the argument could also be passed with docker run instead of during the docker build phase?

That is also possible, if it makes more sense to give FOO a value at runtime:

docker run -e FOO=$(...) ...

Solution 2

This simple technique works for me:

FROM node:9
# ...
ENTRYPOINT dkr2g run "$dkr2g_run_args"

then we launch the container with:

docker run \
    -e dkr2g_run_args="$run_args" \
    --name "$container_name" "$tag_name"

there might be some edge case issues with spreading an env variable into command line arguments, but should work for the most part.

ENTRYPOINT can work either like so:

ENTRYPOINT ["foo", "--bar", "$baz"]  # $baz will not be interpreted

or like so:

ENTRYPOINT foo --bar $baz

not sure why the latter is not preferred - but env variable interpolation/interpretation is only possible using the latter. See: How do I use Docker environment variable in ENTRYPOINT array?

However, a more robust way of passing arguments is to use $@ instead of an env variable. So what you should do then is override --entrypoint using the docker run command, like so:

docker run --entrypoint="foo" <tag> --bar $@

To learn the correct syntax of how to properly override entrypoint, you have to look that up, to be sure, but in general it's weird - you have to put --entrypoint="foo" before the tag name, and the arguments to --entrypoint, after the tag name. weird.

Share:
13,814

Related videos on Youtube

Alexander Mills
Author by

Alexander Mills

Dev, Devops, soccer coach. https://www.github.com/oresoftware

Updated on June 04, 2022

Comments

  • Alexander Mills
    Alexander Mills almost 2 years

    Say I have this in a Dockerfile:

    ARG FOO=1
    ENTRYPOINT ["docker.r2g", "run"]
    

    where I build the above with:

    docker build -t "$tag" --build-arg FOO="$(date +%s)" .
    

    is there a way to do something like:

    ENTRYPOINT ["docker.r2g", "run", ARG FOO]  // something like this
    

    I guess the argument could also be passed with docker run instead of during the docker build phase?