Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker exec quoting variables

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

like image 244
Ben Avatar asked Jan 30 '19 22:01

Ben


People also ask

Does docker inherit environment variables?

Using docker-compose , you can inherit env variables in docker-compose. yml and subsequently any Dockerfile(s) called by docker-compose to build images. This is useful when the Dockerfile RUN command should execute commands specific to the environment.

What is the difference between docker run and Docker exec?

What's the Difference between Docker Run and Docker Exec? Docker Run vs Docker Exec! This is a fairly common question – but has a simple answer! In short, docker run is the command you use to create a new container from an image, whilst docker exec lets you run commands on an already running container!

How do I execute a docker container as a root?

Docker Exec as Root In some cases, you are interested in running commands in your container as the root user. In order to execute a command as root on a container, use the “docker exec” command and specify the “-u” with a value of 0 for the root user.


1 Answers

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
....
like image 172
Ben Avatar answered Oct 04 '22 16:10

Ben