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