jchysk
jchysk

Reputation: 1588

How can I run a complex docker command from host machine that runs in the docker host?

Let's say I have a Host machine and a Vagrant Virtualbox that is running Docker. If I want to run a docker command on the vagrant I can do something along the lines of:

vagrant ssh -c "docker ps"

If I want to remove all the containers I would from within the vagrant be able to run:

docker rm $(docker ps -a -q)

Trying to remove all the containers from outside the vagrant though with:

vagrant ssh -c "docker rm $(docker ps -a -q)"

Does not work. It tries to run the "docker ps -a -q" on the host machine instead of in the Vagrant which won't work. If I instead try:

vagrant ssh -c "docker rm $(vagrant ssh -c \"docker ps -a -q\")"

I get a little bit closer, but not quite working. How can I run a command like this without having to enter the vagrant directly or have a shell script to run?

Upvotes: 1

Views: 510

Answers (1)

Ben Whaley
Ben Whaley

Reputation: 34456

Try using single quotes around the command which will prevent interpolation by your shell before it can be run on the vagrant box.

vagrant ssh -c 'docker rm $(docker ps -a -q)'

Upvotes: 4

Related Questions