Created
July 30, 2023 18:15
-
-
Save murtuzaalisurti/f8f803aee3052d796f3ec4fa87bd59a0 to your computer and use it in GitHub Desktop.
An example shell script to execute docker commands using flags!
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
#!/usr/bin/env bash | |
usage() { | |
echo "Usage: | |
$0 [-b] [-r containerName imageName] [-c (up | down)] [-s (containerName | id)] [-x (containerName | id)] [-d (containerName | id)] [-h] | |
example: $0 -b --- [to build the image] | |
$0 -c up --- [to create and start a container using docker compose] | |
-h: help | |
-b: build the docker image | |
-c: use docker-compose to run the container | |
up: start | |
down: stop | |
-r: run the image in a container and start | |
containerName, imageName | |
-s: start the stopped container | |
containerName/id: it can be name or id | |
-x: stop the container | |
containerName/id: it can be name or id | |
-d: remove the container | |
containerName/id: it can be name or id ">&2; | |
exit 1; | |
} | |
[ $# -eq 0 ] && usage | |
while getopts 'bc:r:s:x:d:h' opt; do | |
case "$opt" in | |
b) | |
docker build -t murtuzaalisurti/notion-dev-articles:1.0 . | |
;; | |
c) | |
if [[ ("$OPTARG" == "up") ]]; then | |
docker-compose up -d | |
elif [[ ("$OPTARG" == "down") ]]; then | |
docker-compose down | |
else | |
usage | |
fi | |
;; | |
r) | |
args+=("$OPTARG") | |
docker run -d -p5000:3000 --env-file ./.env --name ${args[0]} ${args[1]} | |
;; | |
s) | |
docker start $OPTARG | |
;; | |
x) | |
docker stop $OPTARG | |
;; | |
d) | |
docker rm $OPTARG | |
;; | |
h) | |
usage | |
;; | |
?) | |
usage | |
;; | |
esac | |
done | |
shift "$(($OPTIND -1))" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment