Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker: Stop and delete docker container if it's running

I am looking to pragmatically stop and delete a docker container if it is running. This is for a build script.

Take the following example. How would I stop and delete the docker container "rabbitmq" as seen under the NAMES column in a bash script?

docker ps  CONTAINER ID        IMAGE             COMMAND                  CREATED             STATUS              PORTS                   NAMES 9909a5e2856f        rabbitmq-image   "/docker-entrypoint.s"   11 minutes ago      Up 11 minutes       0.0.0.0:5672->5672/tcp, rabbitmq 8990dd1fe503        redis-image      "/entrypoint.sh redis"   6 weeks ago         Up 4 days           0.0.0.0:32770->6379/tcp redis etc  

The following command will delete the container and does what I'm looking to do

docker stop rabbitmq && docker rm -f rabbitmq 

However, it's combing it into a script that I would like to know? I think it would look something like this.

#!/bin/bash  if [ /*docker ps check some value */ ]; then    docker stop rabbitmq && docker rm -f rabbitmq fi 
like image 891
Robbo_UK Avatar asked Dec 11 '15 17:12

Robbo_UK


People also ask

How do I stop a docker container while running?

docker rm -f The final option for stopping a running container is to use the --force or -f flag in conjunction with the docker rm command. Typically, docker rm is used to remove an already stopped container, but the use of the -f flag will cause it to first issue a SIGKILL.

Can we delete docker image if container is running?

You cannot remove an image of a running container unless you use the -f option. To see all images on a host use the docker image ls command.

How do you delete a container after running?

Remove a container upon exiting If you know when you're creating a container that you won't want to keep it around once you're done, you can run docker run --rm to automatically delete it when it exits: Run and Remove: docker run --rm image_name.


2 Answers

As you have probably noticed, docker stop as well as docker rm exit with a status code indicating failure if the container is not existent or not running. This results in your build failing.

If you can cope with the error messages in your build log you can do this little trick to prevent the shell command of failing:

docker stop rabbitmq || true && docker rm rabbitmq || true 

In the case that one of the docker command fails, true is called which always exits with a status code indicating success.

like image 73
Johannes Barop Avatar answered Oct 09 '22 08:10

Johannes Barop


I have a similar problem, but didn't like the accepted answer as it suppresses all errors from the commands, rather than just the "not found" error.

However, docker ps -q --filter "name=rabbitmq" only produces output if a container of that name actually exists, so inspired by Test if a command outputs an empty string I came up with:

docker ps -q --filter "name=rabbitmq" | grep -q . && docker stop rabbitmq && docker rm -fv rabbitmq 

The following command is also useful for testing filter definitions:

docker ps -q --filter "name=rabbitmq" | grep -q . && echo Found || echo Not Found 

My actual use case was in defining a pair of Ansible tasks that deleted all currently existing containers (whether running or not) from a list of names generated in an earlier task:

- name: Check for containers that actually exist   shell: 'docker ps -aq --filter "name={{ item }}"'   with_items:     - '{{ previous_command.stdout_lines }}'   register: found_containers  - name: Remove the containers found by the above command   shell: 'docker stop {{ item.item }} && docker rm -fv {{ item.item }}'   with_items: '{{ found_containers.results }}'   when: item.stdout 
like image 21
ncoghlan Avatar answered Oct 09 '22 08:10

ncoghlan