Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing arguments to docker exec from bash prompt and script

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"
like image 765
curiousengineer Avatar asked Apr 26 '17 23:04

curiousengineer


People also ask

How do I pass args to entrypoint docker?

Just add the full ab command at the end of the docker run command. It will override the whole CMD specified in the Dockerfile. So if you want to pass the URL argument to ENTRYPOINT, you need to pass the URL alone. The reason is we have the ab command as part of the ENTRYPOINT definition.

Can we use CMD and entrypoint together?

Example of using CMD and ENTRYPOINT togetherIf both ENTRYPOINT and CMD are present, what is written in CMD is executed as an option of the command written in ENTRYPOINT. If an argument is added at the time of docker run , the contents of CMD will be overwritten and the ENTRYPOINT command will be executed.

What is the difference between CMD and entrypoint?

Differences between CMD & ENTRYPOINT CMD commands are ignored by Daemon when there are parameters stated within the docker run command while ENTRYPOINT instructions are not ignored but instead are appended as command line parameters by treating those as arguments of the command.


2 Answers

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.

like image 82
Stuart Holliday Avatar answered Oct 23 '22 20:10

Stuart Holliday


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()'.

like image 23
BMitch Avatar answered Oct 23 '22 20:10

BMitch