feat: flag to automatically remove existing containers (with the same tag)

This commit is contained in:
17ms 2024-07-15 20:22:51 +03:00
parent 1547cd2c6e
commit 3c6a54aeee
Signed by untrusted user who does not match committer: ae
GPG Key ID: 995EFD5C1B532B3E

View File

@ -1,18 +1,40 @@
#!/usr/bin/env bash #!/usr/bin/env bash
echo "[+] Starting the deployment script" AUTOREMOVE=false
VOLUME_PATH="./data" # Local path to the volume's mount point
while getopts ":hy" opt
do
case "$opt" in
h)
echo "Usage: $0 [-y]"
exit 0
;;
y)
echo -e "[+] Automatically removing all containers with the same tag (if any)\n"
AUTOREMOVE=true
;;
*)
exit 1
;;
esac
done
! command -v docker &> /dev/null && echo "[!] Docker could not be found, exiting..." && exit 1 ! command -v docker &> /dev/null && echo "[!] Docker could not be found, exiting..." && exit 1
# Building with '--no-cache' ensures a fresh build will always be used # Building with '--no-cache' ensures a fresh build will always be used
echo -e "\n[+] Building the Docker image without caching..." echo -e "[+] Building the Docker image without caching...\n"
docker build --no-cache -t chainmapper . docker build --no-cache -t chainmapper .
[ ! -d "./data" ] && mkdir data && echo -e "\n[+] Created the default volume directory 'data'" [ ! -d "./data" ] && mkdir data && echo -e "\n[+] Created the default volume directory 'data'"
OLD_ID=$(docker ps -a -q -f name="chainmapper-prod") OLD_ID=$(docker ps -a -q -f name="chainmapper-prod")
if [ "$OLD_ID" ] if [ "$OLD_ID" ] && [ "$AUTOREMOVE" = true ]
then
echo -e "\n[+] Removing existing container with the same tag ($OLD_ID)"
docker rm "$OLD_ID" &> /dev/null
elif [ "$OLD_ID" ]
then then
read -p "[?] Existing container found with id '$OLD_ID', do you want to remove it? " -n 1 -r read -p "[?] Existing container found with id '$OLD_ID', do you want to remove it? " -n 1 -r
[[ "$REPLY" =~ ^[Yy]$ ]] || (echo "[!] Exiting..." && exit 0) [[ "$REPLY" =~ ^[Yy]$ ]] || (echo "[!] Exiting..." && exit 0)
@ -20,4 +42,4 @@ then
fi fi
echo -e "\n[+] Deploying the container with 'docker run' ('data' as the volume)..." echo -e "\n[+] Deploying the container with 'docker run' ('data' as the volume)..."
docker run -it --restart unless-stopped -v ./data:/app/data --name chainmapper-prod -d chainmapper docker run -it --restart unless-stopped -v $VOLUME_PATH:/app/data --name chainmapper-prod -d chainmapper