Created
March 14, 2017 03:06
-
-
Save Thrilleratplay/8b1e6392c02abbf8ed0aaf53c8ceb2b6 to your computer and use it in GitHub Desktop.
Docker run/build wrapper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
############################################################################## | |
# SET ME!!! | |
IMAGE_NAME="YOUR_IMAGE_NAME" | |
CONTAINER_NAME="YOUR_CONTAINER_NAME" | |
IS_INTERACTIVE=false; # true/false | |
#VOLUME_INTERNAL_PATH="/root/" | |
#VOLUME_EXTERNAL_PATH=$(pwd)"/" | |
############################################################################## | |
# If container is interactive | |
if [ "$IS_INTERACTIVE" = true ] ; then | |
INTERACTIVE_START_FLAGS="-i" | |
INTERACTIVE_RUN_FLAGS="-i -t" | |
fi | |
# Assemble VOLUME PATH BINDING | |
if [[ $VOLUME_INTERNAL_PATH && $VOLUME_EXTERNAL_PATH ]]; then | |
VOLUME_BINDING = "-v $VOLUME_EXERNAL_PATH:$VOLUME_INTERNAL_PATH" | |
fi | |
# Build image if needed | |
IMAGE_EXISTS=$(docker images -q $IMAGE_NAME) | |
if [ $? -ne 0 ]; then | |
echo "docker command not found" | |
exit $? | |
elif [[ -z $IMAGE_EXISTS ]]; then | |
echo "Building Docker image $IMAGE_NAME..." | |
docker build --no-cache --rm -t "$IMAGE_NAME" ./ | |
fi | |
# With the given name $CONTAINER_NAME, reconnect to running container, start | |
# an existing/stopped container or run a new one if one does not exist. | |
IS_RUNNING=$(docker inspect -f '{{.State.Running}}' $CONTAINER_NAME 2>/dev/null) | |
if [[ $IS_RUNNING == "true" ]]; then | |
docker attach $CONTAINER_NAME | |
elif [[ $IS_RUNNING == "false" ]]; then | |
docker start $INTERACTIVE_START_FLAGS $CONTAINER_NAME | |
else | |
docker run $VOLUME_BINDING $INTERACTIVE_RUN_FLAGS --name $CONTAINER_NAME $IMAGE_NAME | |
fi | |
exit $? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment