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
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…