From f01102fd3432085367afe81f2afe2776200881dc Mon Sep 17 00:00:00 2001 From: Erik Wramner Date: Sun, 28 Jul 2019 14:33:52 +0200 Subject: [PATCH] Connect to running container if -c is used --- setup.sh | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/setup.sh b/setup.sh index c5c64b74..4f7dbf5e 100755 --- a/setup.sh +++ b/setup.sh @@ -12,6 +12,7 @@ INFO=$(docker ps \ IMAGE_NAME=$(echo $INFO | awk '{print $1}') CONTAINER_NAME=$(echo $INFO | awk '{print $2}') DEFAULT_CONFIG_PATH="$(pwd)/config" +USE_CONTAINER=false _update_config_path() { VOLUME=$(docker inspect $CONTAINER_NAME \ @@ -98,14 +99,21 @@ _docker_image_exists() { } _docker_image() { - if ! _docker_image_exists "$IMAGE_NAME"; then - echo "Image '$IMAGE_NAME' not found. Pulling ..." - docker pull "$IMAGE_NAME" - fi + if [ "$USE_CONTAINER" = true ]; then + # Reuse existing container specified on command line + docker exec -ti "$CONTAINER_NAME" "$@" + else + # Start temporary container with specified image + if ! _docker_image_exists "$IMAGE_NAME"; then + echo "Image '$IMAGE_NAME' not found. Pulling ..." + docker pull "$IMAGE_NAME" + fi + docker run \ --rm \ -v "$CONFIG_PATH":/tmp/docker-mailserver \ -ti "$IMAGE_NAME" $@ + fi } _docker_container() { @@ -121,6 +129,7 @@ while getopts ":c:i:p:" OPT; do case $OPT in c) CONTAINER_NAME="$OPTARG" + USE_CONTAINER=true # Container specified, connect to running instance ;; i) IMAGE_NAME="$OPTARG"