I use docker container stop xxx
. It will cause jenkins fail if docker container xxx is not existed.
I want to know if there is an argument to stop a container without failing.It means when container exists, delete it, otherwise do nothing.
You can use the below command :
docker container ps -all --filter "id=XXXXXX" --format {{.ID}} |xargs --no-run-if-empty docker container stop
Explanation :
docker container ps -all
: This command will list all the containers.
docker container ps -all --filter "id=XXXXXX"
: This command will list the container with container id = XXXXXX.
docker container ps -all --filter "id=XXXXXX" --format {{.ID}}
: This command will only print out the container id.
We will use this container id to pass it to the xargs
command to run the docker container stop.
xargs --no-run-if-empty docker container stop
: This command will stop docker container only if command before "|"
(pipe) is non-empty.
use this :
docker stop CONTAINER_NAME 2> /dev/null || true
will always give you exit code 0
You can manage in a simple way as mentioned below:
docker stop XXXX || true && docker rm XXXX || true
This will return true in either case and it will move out with success.
Reference in details at : stop and delete docker container if its running