Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Evaluate bash expression inside container, not host, when using docker exec

Tags:

docker

laravel

I am currently trying to run this command from my docker host:

docker exec container_name /bin/bash -c "touch .env; APP_KEY=$(php artisan key:generate | grep -o '\[.*]' | sed 's/[][]//g');"

Problem is that the expression $(expression) will be evaluated by the host, where php is not in the path, like it should be.

How do I tell docker that it should use container shell to evaluate the expression in the container? Or is there maybe another workaround to accomplish this?

like image 358
metanerd Avatar asked Mar 14 '26 20:03

metanerd


1 Answers

The solution is to use single quotes rather than double quotes:

docker exec container_name /bin/bash -c 'touch .env; APP_KEY=$(php artisan key:generate | grep -o '\[.*]' | sed 's/[][]//g');'

In this case, $ expressions won't be expanded in the host command.

like image 114
yamenk Avatar answered Mar 17 '26 10:03

yamenk