I need to run a command into a chromedriver container passing an ip from a php-fpm container. The php-fpm container hostname is ‘php-fpm’. Both containers are in the same network. So this guys can see each other.
If I access the chromedriver container and run this command, it works properly:
chromedriver --url-base=/wd/hub --whitelisted-ips=$(getent hosts php-fpm | cut -d' ' -f1)
What I expect here is $(getent hosts php-fpm | cut -d' ' -f1)
grab the ip from php-fpm container and assign to whitelisted-ips param. Again, it works properly.
But from my host machine if I run:
docker-compose exec chromedriver chromedriver --url-base=/wd/hub --whitelisted-ips=$(getent hosts php-fpm | cut -d' ' -f1)
The getent command don’t return the ip of php-fpm. Is there any consideration of escaping chars, extra parameter or something like that to this exec command works?
I can run the chromedriver without ip. Is only for developing enviroments. But I would like to know why this command doesn’t works.
Thanks advance.
Advertisement
Answer
This fails because the $(..)
runs on your host machine, and the result of it is passed into the docker image.
To make it happen inside the docker image, you can pass the command unexpanded to a shell in the image, and have that shell expand and run the command:
docker-compose exec chromedriver bash -c "chromedriver --url-base=/wd/hub --whitelisted-ips=$(getent hosts php-fpm | cut -d' ' -f1)"