12

I'd like to know if there's a way to do this

Let's say the dockerfile contains this line, that specifies path of an executable

ENV CLI /usr/local/bin/myprogram

I'd like to be able to call this program using ENV variable name through exec command.

For example docker exec -it <my container> 'echo something-${CLI} Expecting something-/usr/local/bin/myprogram

However that returns:

OCI runtime exec failed: exec failed: container_linux.go:348: starting container process caused "exec: \"${CLI} do something\": executable file not found in $PATH": unknown

2 Answers 2

22

Ok, I found a way to do it, all you need to do is evaluate command with bash

docker exec -it <container id> bash -c 'echo something-${CLI}'

returns something-/usr/local/bin/myprogram

If the CLI environment variable is not already set in the container, you can also pass it in such as:

docker exec -it -e CLI=/usr/local/bin/myprogram <container id> bash -c 'echo something-${CLI}'

See the help file:

 docker exec --help

 Usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...]

 Run a command in a running container

Options:
-d, --detach               Detached mode: run command in the background
-e, --env list             Set environment variables
....
3
  • 1
    I stand by my statement that I question your ability to understand even your own answer. Your original request (before your edits) was docker exec -it <my container> '${CLI} do something', this will not work, usage: docker exec [OPTIONS] CONTAINER COMMAND [ARG...], "COMMAND should be an executable, a chained or a quoted command will not work" (ref. docs.docker.com/engine/reference/commandline/exec).
    – masseyb
    Jan 31, 2019 at 0:55
  • 1
    @masseyb well you haven't understood the question then
    – Ben
    Jan 31, 2019 at 4:47
  • keep telling yourself that. 1 edit that only changes the expected results, I mean for sure, everyone should be able to guess what you’re actually expecting amirite. Also, love the double standard, « the proper answer is x because ... <explanation> ». How about providing an explanation for your answer?
    – masseyb
    Jan 31, 2019 at 16:21
-3

In it's original revision docker exec -it <my container> '${CLI} do something' with the expectation that ${CLI} will be substituted with /usr/local/bin/myprogram (as the exec COMMAND) and everything after passed as ARG's to /usr/local/bin/myprogram will not work, this is clearly documented: https://docs.docker.com/engine/reference/commandline/exec/

COMMAND should be an executable, a chained or a quoted command will not work. Example:

docker exec -ti my_container "echo a && echo b" will not work, but

docker exec -ti my_container sh -c "echo a && echo b" will.

Following the documentation, this will work as expected: docker exec -ti my_container sh -c "${CLI} foo", ${CLI} will be be executed after variable expansion and the argument(s) passed to the shell script set in ${CLI} (e.g. sh -c /usr/local/bin/myprogram foo).

Alternatively you could set the ENTRYPOINT to your script and pass in arguments with CMD or at the command line with docker run for example:

Given the below directory structure:

.
├── Dockerfile
└── example.sh

The Dockerfile contents:

FROM ubuntu:18.04
COPY example.sh /bin
RUN chmod u+x /bin/example.sh
ENTRYPOINT ["/bin/example.sh"]
CMD ["bla"]

And the example.sh script contents:

#!/bin/bash
echo $1

The CMD specified in the Dockerfile after the ENTRYPOINT will be the default argument for your script and you can override the default argument on the command line (assuming that the image is built and tagged as example:0.1):

user@host> docker run --rm example:0.1
bla
user@host> docker run --rm example:0.1 "arbitrary text"
arbitrary text

Note: this is my go to article for differences between ENTRYPOINT and CMD in Dockerfile's: https://medium.freecodecamp.org/docker-entrypoint-cmd-dockerfile-best-practices-abc591c30e21

7
  • I'm not sure if you understand my question, as your proposed solution has no relevance
    – Ben
    Jan 30, 2019 at 23:04
  • @Ben my answer to your question of ~"if it's possible" is no, I don't believe so on the basis that the environment variables specified after the docker exec command appear to be evaluated on the host, e.g. export CLI=random on your host then docker exec -it <your_container> ${CLI} do something without quotes will substitute ${CLI} with the value set on the host (if set in your environment - error: ... process caused "exec: \"random\": executable file not found in $PATH": unknown), not in the container and with quotes the variable is not substituted at all hence your error.
    – masseyb
    Jan 30, 2019 at 23:17
  • Yes, a simple 'no' should be enough, however your proposal with ENTRYPOINT truly has no relevance. I did not asked how to set an executable, all I wanted to do is quoting container local variables in exec context. docker exec -it <my container> 'echo ${CLI}' for example
    – Ben
    Jan 30, 2019 at 23:35
  • @Ben I answered your question in the first line, ignore everything after that if you want, same diff.
    – masseyb
    Jan 30, 2019 at 23:40
  • I don't believe it's going to be possible can not be considered as an answer. It implies that you're not sure. The proper answer is No, because ... <explanation>
    – Ben
    Jan 30, 2019 at 23:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.