2023-02-10 22:37:09 -05:00
#!/bin/sh
2023-02-11 16:59:04 -05:00
2023-02-11 17:14:38 -05:00
# You can split the value of the --images argument using the IFS (Internal Field Separator) variable and the read command,
# and then iterate over the resulting parts using a for loop.
# See more: https://unix.stackexchange.com/questions/184863/what-is-the-meaning-of-ifs-n-in-bash-scripting
IFS = '=' read -ra ARGUMENT_PARTS <<< " $1 "
IFS = '/' read -ra IMAGE_PARTS <<< " ${ ARGUMENT_PARTS [1] } "
2023-02-11 16:59:04 -05:00
# The block below will use iteration to either build the necessary Docker image or simply run the required containers using the provided entrypoint arguments
# First, the image will be created, and then the container will be run using the image that was generated
2023-02-11 17:14:38 -05:00
for IMAGE in " ${ IMAGE_PARTS [@] } "
2023-02-11 16:59:04 -05:00
do
# The following command will display the IDs of Docker images filtered by name
# See more: https://docs.docker.com/engine/reference/commandline/images/
2023-02-11 17:14:38 -05:00
if [ ! " $( docker images -aq --filter 'reference=${IMAGE}' ) " ] ; then
docker buildx build -f scripts/${ IMAGE } .dockerfile -t ${ IMAGE } :latest scripts/
2023-02-11 16:59:04 -05:00
fi
# The following command will achieve the same result as the previous command, but for containers rather than images
# See more: https://docs.docker.com/engine/reference/commandline/ps/
2023-02-11 17:14:38 -05:00
if [ ! " $( docker ps -aq --filter name = ${ IMAGE } ) " ] ; then
[ " $( docker ps -aq --filter status = exited -f name = ${ IMAGE } ) " ] && docker rm ${ IMAGE }
docker run -d --name ${ IMAGE } ${ IMAGE } :latest
2023-02-11 16:59:04 -05:00
fi
done
2023-02-11 13:55:17 -05:00
exec sleep infinity