From 9279b059076a9c56622c655181f481c8976f265e Mon Sep 17 00:00:00 2001 From: zareldyn Date: Tue, 26 Jun 2018 00:33:56 +0200 Subject: [PATCH] Better container management --- CHANGELOG.md | 5 +++++ is-running | 9 ++++++--- stop | 18 +++++++++++++----- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 46b592a..653436a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Error in documentation +### Changed + +- is-running is safer to use +- The stop command always tries to remove the container, whatever is-running says + ## [1.2.0] - 2018-04-08 diff --git a/is-running b/is-running index 41aa822..97c9920 100755 --- a/is-running +++ b/is-running @@ -9,7 +9,10 @@ function say_no { service docker status &>/dev/null || say_no -docker ps | grep -q " $APP_NAME$" -[ $? -eq 0 ] || say_no +status=$(docker inspect -f='{{.State.Status}}' "$APP_NAME" 2>/dev/null) +[ "$status" == "running" ] && { + echo "yes" + exit +} -echo "yes" +say_no diff --git a/stop b/stop index fadc8e8..003cc8f 100755 --- a/stop +++ b/stop @@ -3,10 +3,18 @@ SELF_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" APP_NAME=$(basename "$SELF_DIR") [ $(. "$SELF_DIR"/is-running) == "yes" ] || { - echo "$APP_NAME is already stopped" - exit + docker inspect -f='{{.State.Status}}' "$APP_NAME" &>/dev/null + [ $? -ne 0 ] && { + echo "$APP_NAME is already stopped" + exit + } } -docker stop $APP_NAME >/dev/null && \ -docker rm $APP_NAME >/dev/null && \ -echo "$APP_NAME stopped" +docker stop $APP_NAME >/dev/null +docker rm -f $APP_NAME >/dev/null && { + echo "$APP_NAME stopped" + exit +} + +echo "Error while trying to remove $APP_NAME container" >&2 +exit 1 -- 2.22.0