11

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?

halfer
  • 19,471
  • 17
  • 87
  • 173
Alexander Mills
  • 78,517
  • 109
  • 412
  • 724

2 Answers2

7

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=$(...) ...
VonC
  • 1,129,465
  • 480
  • 4,036
  • 4,755
  • another solution might be to use `--entrypoint` with the docker run command and pass in the flag there? if that works, feel free to add that to your answer, idk – Alexander Mills Jun 19 '18 at 06:47
  • @AlexanderMills Yes, but if your entry point is still "docker.r2g", you can override CMD instead (since CMD are parameters passed to entrypoint) – VonC Jun 19 '18 at 06:52
  • I am not sure if you already saw my answer, but I had to try a lot of things to get this to work properly. Turns out, trying to include a list of arguments in a single env variable is not that robust, so passing arguments using `$@` with bash, is probably the best way. – Alexander Mills Jun 19 '18 at 21:05
  • thanks yeah I figured, I updated with a final addition, using `docker run --entrypoint` we can override ENTRYPOINT which is probably the absolute best way to pass in a dynamic list of arguments. – Alexander Mills Jun 19 '18 at 21:10
5

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.

Alexander Mills
  • 78,517
  • 109
  • 412
  • 724