Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"docker run" command to evaluate bash $variable inside the container

How can I run a command inside a docker container, using docker run, where bash variables are evaluated inside the container?

E.g.:

$ SOMEONE=host
$ docker run --env SOMEONE=busybox busybox echo "Hello $SOMEONE"
Hello host

How can I make it output Hello busybox?

like image 356
Robin Winslow Avatar asked May 14 '26 15:05

Robin Winslow


1 Answers

  • To prevent the replacement from happening from the outer shell, one needs to use single quotes, not double.

  • To ensure that there is an inner shell that can do a replacement (echo doesn't have any such functionality itself!), we need to explicitly call sh -c; otherwise, Docker will just directly invoke execlp("echo", "echo", "$SOMEONE", NUL) inside the container, which doesn't actually do any substitution.


Thus:

docker run --env SOMEONE=busybox busybox sh -c 'echo "Hello $SOMEONE"'
like image 109
Charles Duffy Avatar answered May 17 '26 13:05

Charles Duffy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!