Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass ARG to ENTRYPOINT

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?

like image 768
Alexander Mills Avatar asked Feb 03 '23 23:02

Alexander Mills


2 Answers

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=$(...) ...
like image 154
VonC Avatar answered Mar 05 '23 17:03

VonC


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.

like image 24
Alexander Mills Avatar answered Mar 05 '23 16:03

Alexander Mills