I'm building a sh
script to be able to run Docker containers all in once. In this way, I no need to run single containers every time.
What I need to add now is a way to run my docker-compose up --build -target=<ENV>
the ENV will be DEV or PROD. In this way, I can run the right environment in my Docker setup.
At this time my script looks as follow but when I try to pass $2 = $DEV
is giving me an error of [: =: unary operator expected
and I don't know what could be the right fix to this
#!/bin/bash
CLEAN="clean"
RUN="run"
STOP="stop"
DEV="dev"
PROD="prod"
if [ "$#" -eq 0 ] || [ $1 = "-h" ] || [ $1 = "--help" ]; then
echo "Usage: ./myapp [OPTIONS] COMMAND [arg...]"
echo " ./myapp [ -h | --help ]"
echo ""
echo "Options:"
echo " -h, --help Prints usage."
echo ""
echo "Commands:"
echo " $CLEAN - Stop and Remove containers."
echo " $RUN - Build and Run containers."
echo " $STOP - Stop containers."
exit
fi
clean() {
stop_existing
remove_stopped_containers
remove_unused_volumes
}
run() {
echo "Cleaning..."
clean
echo "Running docker..."
if [ $2 = $DEV ]; then
echo "$DEV - Running in - $DEV - environment"
docker-compose up --build -target=$DEV
fi
}
stop_existing() {
MYAPP="$(docker ps --all --quiet --filter=name=wetaxitask_api_dev)"
REDIS="$(docker ps --all --quiet --filter=name=wetaxitask_redis)"
MONGO="$(docker ps --all --quiet --filter=name=wetaxitask_mongodb)"
if [ -n "$MYAPP" ]; then
docker stop $MYAPP
fi
if [ -n "$REDIS" ]; then
docker stop $REDIS
fi
if [ -n "$MONGO" ]; then
docker stop $MONGO
fi
}
remove_stopped_containers() {
CONTAINERS="$(docker ps -a -f status=exited -q)"
if [ ${#CONTAINERS} -gt 0 ]; then
echo "Removing all stopped containers."
docker rm $CONTAINERS
else
echo "There are no stopped containers to be removed."
fi
}
remove_unused_volumes() {
CONTAINERS="$(docker volume ls -qf dangling=true)"
if [ ${#CONTAINERS} -gt 0 ]; then
echo "Removing all unused volumes."
docker volume rm $CONTAINERS
else
echo "There are no unused volumes to be removed."
fi
}
if [ $1 = $CLEAN ]; then
echo "Cleaning..."
clean
exit
fi
if [ $1 = $RUN ]; then
run
exit
fi
if [ $1 = $STOP ]; then
stop_existing
exit
fi
What I want to achieve is that possible to run my sh as follow
./script.sh run dev or prod