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 im looking to do
docker stop rabbitmq && docker rm -f rabbitmq
However its 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
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.
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
You can use:
app="rabbitmq"
if docker ps | awk -v app="$app" 'NR > 1 && $NF == app{ret=1; exit} END{exit !ret}'; then
docker stop "$app" && docker rm -f "$app"
fi
awkcommand gets a command line varappfrom BASH's variable$appNR>1skips first header row fromdocker pscommand.$(NF) == appCompare last columnNAMESis equal to app variable or not
Been using docker for a while longer now. This is my preferred way to stop and remove a docker container. The piping of true is there to ensure the it always outputs a success. Without it any bash scripts would exit and error if the container name did not exist.
docker rm -f container_name || true
# Stop the running "rabbitmq" container if there is one
CONTAINER_NAME="rabbitmq"
OLD="$(docker ps --all --quiet --filter=name="$CONTAINER_NAME")"
if [ -n "$OLD" ]; then
docker stop $OLD && docker rm $OLD
fi
I suggest this incantation in bash:
( docker stop $CONTAINER > /dev/null && echo Stopped container $CONTAINER && \
docker rm $CONTAINER ) 2>/dev/null || true
It always exits with 0, doesn't complain if the container is not running, and prints Stopped container $CONTAINER if it actually got stopped.
Copy this code in your script.sh if you want stop and remove all
#!/bin/sh
ids=$(docker ps -a -q)
for id in $ids
do
echo "$id"
docker stop $id && docker rm $id
done
If you do not delete your stopped containers, another simple way to address this is to rely on docker ps -a, which will always return that container id. Then executing docker stop on that stopped container will idempotently simply do nothing:
docker stop $(docker ps -a --filter name= rabbitmq -q )
to stop all containers first you have to stop all containers with
docker kill $(docker ps -q)
and to delete all containers
docker rm $(docker ps -a -q)
and if you want delete all images this is the command
docker rmi $(docker images -q)
来源:https://stackoverflow.com/questions/34228864/stop-and-delete-docker-container-if-its-running