Skip to content

Instantly share code, notes, and snippets.

@BilalAM
Created November 17, 2018 20:25
Show Gist options
  • Save BilalAM/4921e0f86f4e3431d7405ac9a4b61d36 to your computer and use it in GitHub Desktop.
Save BilalAM/4921e0f86f4e3431d7405ac9a4b61d36 to your computer and use it in GitHub Desktop.
Script for quick docker access (obviously this gist is highly subjected to changes)
#!/bin/bash
imageName=$1
interactiveAndTerminalMode=$2
function checkIfImageExistsOnRepo() {
echo $(curl -o /dev/null -s -w "%{http_code}\n" "https://hub.docker.com/_/$imageName/")
}
function pullAndStartPlainImage() {
echo "[LOG] Pulling Image $imageName From Docker Repository."
local statusCode=$(checkIfImageExistsOnRepo)
if [[ $statusCode -ne 200 ]]; then
echo "[ERROR] Cannot Find Image $1 From Online Repository ! , Exiting..."
exit 1;
fi
docker pull $imageName
echo "[LOG] Successfully Pulled Image $1 From Docker Repository."
echo "[LOG] Running $imageName"
docker run $imageName
}
function pullAndStartInteractiveImage() {
echo "[LOG] Pulling Image $imageName From Docker Repository."
checkIfImageExistsOnRepo
if [[ $statusCode -ne 300 ]]; then
echo "[ERROR] Cannot Find Image $1 From Online Repository ! , Exiting..."
exit 1;
fi
docker pull $imageName
echo "[LOG] Successfully Pulled Image $1 From Docker Repository."
echo "[LOG] Running $1"
docker run $imageName -it
}
if [[ $# -lt 1 ]]; then
echo "[ERROR] Incorrect Number Of Arguments $# Provided ! "
exit 0
elif [[ $# -eq 1 ]]; then
pullAndStartPlainImage
exit 0
elif [[ $# -eq 2 ]]; then
if [[ $2 = "true" ]]; then
pullAndStartInteractiveImage
exit 0
#statements
elif [[ $2 = "false" ]]; then
pullAndStartPlainImage
exit 0;
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment