Skip to content

Instantly share code, notes, and snippets.

@ruslanguns
Created November 2, 2019 15:29
Show Gist options
  • Save ruslanguns/11d584152c0eb5301fbb08cdd65c3500 to your computer and use it in GitHub Desktop.
Save ruslanguns/11d584152c0eb5301fbb08cdd65c3500 to your computer and use it in GitHub Desktop.
NestJS Docker Script for shorter commands
#!/usr/bin/env bash
# Initialise option flag with a false value
OPT_RUN='false'
OPT_BUILD='false'
OPT_TEST='false'
# Process all options supplied on the command line
while getopts ':r, :b, :t' 'OPTKEY'; do
case ${OPTKEY} in
'r')
# Update the value of the option x flag we defined above
OPT_RUN='true'
;;
'b')
OPT_BUILD='true'
;;
't')
OPT_TEST='true'
;;
'?')
echo "INVALID OPTION -- ${OPTARG}" >&2
exit 1
;;
':')
echo "MISSING ARGUMENT for option -- ${OPTARG}" >&2
exit 1
;;
*)
echo "UNIMPLEMENTED OPTION -- ${OPTKEY}" >&2
exit 1
;;
esac
done
# [optional] Remove all options processed by getopts.
shift $(( OPTIND - 1 ))
[[ "${1}" == "--" ]] && shift
# PLEASE NOTE THAT I AM USING WINDOWS docker.exe if you're running linux make sure you put the script correctly.
# "do one thing if -X is specified and another if it is not"
if ${OPT_RUN}; then
echo -e "Running your Server Instance ...\n\n"
docker.exe rm -f configtut
docker.exe run --rm --name configtut -it -p 3000:3000 configtut /sbin/my_init -- bash -l
elif ${OPT_BUILD}; then
echo -e "Building your Server ...\n\n"
docker.exe build -t configtut .
elif ${OPT_TEST}; then
echo -e "Testing your Development Instance ...\n\n"
docker.exe rm -f configtut
docker.exe run --rm --name configtut -it -e 'DB_USERNAME=realProdUser' -e 'DB_PASSWORD=realProdPassword' -e 'DB_NAME=realProdDb' -p 3000:3000 configtut /sbin/my_init -- bash -l
else
echo -e "Options not supplied on the command line\nYou must specify ONLY ONE valid parameters\n\nValid options are:"
echo -e " -d: Build Docker"
echo -e " -d: Test Docker"
echo -e " -r: Start Docker Server \n"
fi
@ruslanguns
Copy link
Author

ruslanguns commented Nov 2, 2019

I created this script to fix a WSL Permissions issues when I am running Docker Commands from NPM Run.

Also this can be serve as an example for building logic scripts for making your life easier.

The way it works is very simple:

Just execute the script from bash ./docker.exe and then you will be prompt with the allowed options.

"scripts": {
   ...
    "build:docker": "./scripts/docker.sh -b",
    "start:docker": "./scripts/docker.sh -r",
    "start:docker-testenv": "./scripts/docker.sh -t",
   ...
  },

If you want to modify it please make sure you are selecting the End of Line Sequence LF.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment