Reputation: 2607
I am trying to execute a command inside my mongodb docker container. From my linux command prompt it is pretty easy and works when I do this
docker exec -it d886e775dfad mongo --eval 'rs.isMaster()'
The above tells me to go to a container and execute the command
"mongo --eval 'rs.isMaster()' - This tells mongo to take rs.isMaster() as an input and execute it. This works and gives me the output.
Since I am trying to automate this via bash script, I did this
cmd="mongo --eval 'rs.isMaster()"
And then tried executing like this
docker -H $node2 exec d886e775dfad "$cmd"
But I guess somehow docker thinks that now the binary file inside the container is not mongo or sth else and it gives me the following error:
rpc error: code = 2 desc = oci runtime error: exec failed: container_linux.go:247: starting container process caused "exec: \"mongo --eval 'rs.isMaster()'\": executable file not found in $PATH"
Upvotes: 15
Views: 36899
Reputation: 401
docker exec -it d886e775dfad sh -c "mongo --eval 'rs.isMaster()'"
This calls the shell (sh
) executing the script in quotation marks.
Note that this also fixes things like wildcards (*) which otherwise do not work properly with docker exec.
Upvotes: 29
Reputation: 263856
You need to run (there's a missing single quote in your example):
cmd="mongo --eval 'rs.isMaster()'"
Followed by (without the quotes around $cmd
):
docker -H $node2 exec d886e775dfad $cmd
By including quotes around $cmd
you were looking for the executable file mongo --eval 'rs.isMaster()'
rather than the executable mongo
with args mongo
, --eval
, and 'rs.isMaster()'
.
Upvotes: 4
Reputation: 218
It looks as though mongo is interpreting the entire contents of $cmd
as the command to execute. This makes sense because you are quoting the parameter on the command line. Essentially when you did ... exec "$cmd"
the shell will interpret the dollar-sign and expand it to be the contents in-place. Next it will interpret the double-quotes and pass the entire inner contents (now being mongo --eval 'rs.isMaster()'
as a single command line argument to the process. This should result in mongo looking for a program with the name mongo --eval 'rs.isMaster()
in your path. Which obviously doesn't exist.
Upvotes: 0