Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop and remove docker container with force ignore

Tags:

bash

docker

How can I delete a docker container with force ignore that means if there is no running docker container it should do nothing and if there is a running docker container with the name then it should stop and remove that container.

I am executing the following code to stop and remove the container.

docker rm -f CONTAINER_NAME || true

If there is a running container then everything works fine, however, if there is no container then the following error is thrown:

Error: No such container: CONTAINER_NAME

Is there something like --force ignore? I need this behaviour in order to include it in an automated Makefile.

like image 713
Rene B. Avatar asked Jan 31 '26 06:01

Rene B.


1 Answers

try this exit code will be 1:

docker rm -f CONTAINER_NAME 2> /dev/null

this with exit code 0:

docker rm -f CONTAINER_NAME 2> /dev/null || true
like image 178
LinPy Avatar answered Feb 01 '26 21:02

LinPy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!