3

I want to access the value of one of environment variable in my dockerfile , and pass it as first argument to the main script in docker ENTRYPOINT.

I came across this so link which shows two ways to do it. one with exec form and one with shell form.

The exec form worked fine to echo the environment variable with ["sh", "-c", "echo $VARIABLE"] but when I tried with my custom entrypoint script ENTRYPOINT ["/bin/customentrypoint.sh", "$VARIABLE"] it is not able to get the value for variable, instead its just taking it as constant $VARIABLE.

So I went with shell form approach and just called ENTRYPOINT /bin/customentrypoing "$VARIABLE", and it worked fine to get the value of $VARIABLE but It seems that its restricting the no of command line arguments in this case. as I am getting only one value of $@ even after passing other command line arguments from docker run.Can someone please help me if I am doing something wrong , or I should tackle this in different way.Thanks in Advance.

docker looks is similar to

#!/usr/bin/env bash
...
ENV VARIABLE NO
...
RUN echo "#!/bin/bash" > /bin/customentrypoint.sh
RUN echo "if  [ "\"\$1\"" = 'YES' ] ; then ; python ${LOCATION}/main.py" \"\$@\" "; else ; echo Please select -e VARIABLE=YES ; fi" >> /bin/customentrypoint.sh
RUN chmod +x /bin/customentrypoint.sh
RUN ln -s -T /bin/customentrypoint.sh /bin/customentrypoint
WORKDIR ${LOCATION}
ENTRYPOINT /bin/customentrypoint "$VARIABLE"         # - works fine but limits no of command line arguments
# ENTRYPOINT ["bin/customentrypoint", "$VARIABLE"]   # not able to get value of $VARIABLE instead taking as constant.

command I am using

docker run --rm -v $PWD:/mnt -e VARIABLE=VALUE docker_image:tag entrypoint -d /mnt/tmp -i /mnt/input_file 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
lazarus
  • 677
  • 1
  • 13
  • 27
  • Maybe the issue is you're doing `$VARIABLE` instead of `$ENVIRONMENT` in the original exec form? – Itamar Turner-Trauring Mar 27 '20 at 17:24
  • You don't need to pass environment variables as positional parameters; you can just directly access `$VARIABLE` from within the entrypoint script. – David Maze Mar 27 '20 at 17:25
  • @DavidMaze , I wanted to change values of `$VARIABLE` in the runtime with ```docker run``` , If I am accessing `$VARIABLE` from within the entrypoint script , I am getting the fixed value which I had during `docker build` – lazarus Mar 27 '20 at 17:29
  • @ItamarTurner-Trauring , sorry Just updated the question, using `$VARIABLE` in command as well – lazarus Mar 27 '20 at 17:32
  • The entrypoint doesn't get evaluated or executed until after the `docker run -e` options take effect; you should be able to override `$VARIABLE` at run time. Including a [mcve] including the Dockerfile and entrypoint script might make this clearer. – David Maze Mar 27 '20 at 17:43
  • Thanks @DavidMaze I have updated with a scaffold of dockerfile I am using. – lazarus Mar 27 '20 at 17:59

1 Answers1

1

The environment for CMD is interpreted slightly differently depending on how you write the arguments. If you pass the CMD as a string (not inside an array), it gets launched as a shell instead of exec. See https://docs.docker.com/engine/reference/builder/#cmd.

What you can try if you want to use array is

ENTRYPOINT ["/bin/sh", "-c", "echo ${VARIABLE}"]
nischay goyal
  • 3,206
  • 12
  • 23
  • thansk , but this is already working fine ,my main question was to get with `ENTRYPOINT ['bin/customerentrypoint, "echo $VARIABLE"]` I have already gone through https://goinbigdata.com/docker-run-vs-cmd-vs-entrypoint/ – lazarus Mar 30 '20 at 05:19
  • As you can see in my answer, I have already added the ENTRYPOINT format to achieve that. – nischay goyal Mar 30 '20 at 08:31