Using variable interpolation in string in Docker

I would avoid using variable in ENTRYPOINT at all. It’s tricky and requires a deep understanding of what is going on. And is easy to break it by accident. Just consider one of the following.

Create link with the known name to your start script.

RUN ln -s /home/newuser/$sname /home/newuser/docker_entrypoint.sh
ENTRYPOINT ["/home/newuser/docker_entrypoint.sh"]

or write standalone entrypoint script that runs what you need.

But if you want to know how and why solutions in your questions work just keep reading.

First some definitions.

  • ENV – is environment variable available during buildtime (docker build) and runtime (docker run)
  • ARG – is environment variable available only during buildtime

If you look at https://docs.docker.com/engine/reference/builder/#environment-replacement you see the list of dockerfile instructions that support those environment variables directly. This is why COPY “picks up the variable” as you said.

Please note that there is no RUN nor ENTRYPOINT. How does it work?

You need to dig into the documentation. First RUN (https://docs.docker.com/engine/reference/builder/#run). There are 2 forms. The first one executes command through the shell and this shell has access to buildtime environment variables.

# this works because it is called as /bin/sh -c 'echo $sname'
# the /bin/sh replace $sname for you      
RUN echo $sname 

# this does NOT work. There is no shell process to do $sname replacement 
# for you
RUN ["echo", "$sname"]

Same thing applies to the ENTRYPOINT and CMD except only runtime variables are available during container start.

# first you need to make some runtime variable from builtime one
ENV sname $sname

# Then you can use it during container runtime
# docker runs `/bin/sh -c '/bin/bash /home/newuser/$sname'` for you
# and this `/bin/sh` proces interprets `$sname`
ENTRYPOINT /bin/bash /home/newuser/$sname

# but this does NOT work. There is no process to interpolate `$sname`
# docker runs what you describe.
ENTRYPOINT ["/bin/bash", "/home/newuser/$sname"]

edit 2017-04-03: updated links to the docker documentations and slight rewording to avoid confusion that I sense from other answers and comments.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)