docker ps -f health=unhealthy --format "docker restart {{.ID}}" | xargs --no-run-if-empty -t docker restart
output is :
docker restart docker restart 7c20974e2b02
7c20974e2b02
Error response from daemon: No such container: docker
Error response from daemon: No such container: restart
How do I not get the "Error response from daemon: No such container: "
2
Answers
Take
docker restart
out of the format string; it should only be on the argument list.When you had it in both places you were passing the string
docker restart
at least twice whenever xargs invoked anything at all.Alternately, consider doing this without xargs entirely. Assuming that the number of unhealthy nodes is short enough to fit on a single command line — generally a safe assumption, since the combined environment + argument length limit is typically in the range of 128KB on modern systems:
You have both
docker ps
with its--format
option andxargs
puttingdocker restart
in front of the container name. You need to do one or the other. Removing the format option will havedocker ps
only pass the container ID toxargs
which will run it as an additional argument todocker restart
that it already has:Or you can remove restart from xargs and just have format add that by itself:
xargs always requires the base command so you can’t pass
docker
in the format, but all other arguments can be fed into it. I’d recommend the former approach. This latter example is more to demonstrate the difference between each option. Also, ifdocker ps
ever returns more than one ID, you only wantrestart
to appear once as it will in the former case.