Skip to content

Instantly share code, notes, and snippets.

@belano
Last active May 3, 2016 14:55
Show Gist options
  • Save belano/1ef767b9097bfc87a7a7e536c3a09ea7 to your computer and use it in GitHub Desktop.
Save belano/1ef767b9097bfc87a7a7e536c3a09ea7 to your computer and use it in GitHub Desktop.
Helper script for (re)starting a docker container
#!/bin/bash
function getStatus(){
CONTAINER_ID=$(docker ps -a | grep -v Exit | grep $1 | awk '{print $1}')
if [[ -z $CONTAINER_ID ]] ; then
echo 'Not running.'
return 1
else
echo "Running in container: $CONTAINER_ID"
return 0
fi
}
CONTAINER=$1
IMAGE=$2
HOSTPORT=$3
CONTAINERPORT=$4
CONTAINER_ID=$(docker ps -a | grep -v Exit | grep $CONTAINER | awk '{print $1}')
if [ -n "$CONTAINER_ID" ]; then
echo "Running in container: $CONTAINER_ID"
SRV=$(docker stop $CONTAINER_ID)
SRV=$(docker rm $CONTAINER_ID)
if [ $? -eq 0 ] ; then
echo 'Stopped.'
fi
fi
CONTAINER_ID=$(docker run -d -p $HOSTPORT:$CONTAINERPORT --name $CONTAINER $IMAGE)
getStatus $CONTAINER
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment