Connect to running container if -c is used

This commit is contained in:
Erik Wramner 2019-07-28 14:33:52 +02:00
parent f21bffe322
commit f01102fd34

View file

@ -12,6 +12,7 @@ INFO=$(docker ps \
IMAGE_NAME=$(echo $INFO | awk '{print $1}') IMAGE_NAME=$(echo $INFO | awk '{print $1}')
CONTAINER_NAME=$(echo $INFO | awk '{print $2}') CONTAINER_NAME=$(echo $INFO | awk '{print $2}')
DEFAULT_CONFIG_PATH="$(pwd)/config" DEFAULT_CONFIG_PATH="$(pwd)/config"
USE_CONTAINER=false
_update_config_path() { _update_config_path() {
VOLUME=$(docker inspect $CONTAINER_NAME \ VOLUME=$(docker inspect $CONTAINER_NAME \
@ -98,14 +99,21 @@ _docker_image_exists() {
} }
_docker_image() { _docker_image() {
if ! _docker_image_exists "$IMAGE_NAME"; then if [ "$USE_CONTAINER" = true ]; then
echo "Image '$IMAGE_NAME' not found. Pulling ..." # Reuse existing container specified on command line
docker pull "$IMAGE_NAME" docker exec -ti "$CONTAINER_NAME" "$@"
fi 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 \ docker run \
--rm \ --rm \
-v "$CONFIG_PATH":/tmp/docker-mailserver \ -v "$CONFIG_PATH":/tmp/docker-mailserver \
-ti "$IMAGE_NAME" $@ -ti "$IMAGE_NAME" $@
fi
} }
_docker_container() { _docker_container() {
@ -121,6 +129,7 @@ while getopts ":c:i:p:" OPT; do
case $OPT in case $OPT in
c) c)
CONTAINER_NAME="$OPTARG" CONTAINER_NAME="$OPTARG"
USE_CONTAINER=true # Container specified, connect to running instance
;; ;;
i) i)
IMAGE_NAME="$OPTARG" IMAGE_NAME="$OPTARG"