liljudd-website/docker.sh

101 lines
2.4 KiB
Bash
Raw Normal View History

2024-01-05 00:29:42 +00:00
#!/bin/bash
# Script: your_script.sh
# Description: Brief description of what the script does.
# Usage: ./your_script.sh [options]
# Options:
# -h, --help Display this help message
# -o, --option Specify an option
# Function to display usage information
function show_usage() {
echo "Usage: $0 [options]"
echo "Options:"
echo " -h, --help Display this help message"
echo " -d, --deploy Delete possibly old images and container and deploy a new container"
echo " -f, --follow If deployed will follow the log output"
echo " -s, --stay If deployed will leave the container running instead of stopping it"
}
# Check for the number of arguments
# if [ "$#" -eq 0 ]; then
# show_usage
# exit 1
# fi
# Parse command-line options
while [[ "$#" -gt 0 ]]; do
case $1 in
-h|--help)
show_usage
exit 0
;;
-d|--deploy)
deploy=true
;;
-f|--follow)
follow=true
;;
*)
echo "Unknown option: $1"
show_usage
exit 1
;;
esac
shift
done
image_name="solidjs-docker"
container_name="solidjs"
if [ $deploy ]; then
# Check if the container exists
if [ "$(docker ps -a -q -f name=$container_name)" ]; then
if [ "$(docker ps -q -f name=$container_name)" ]; then
# Container exists, stop and remove it
echo "Stopping and removing existing container..."
echo "Stopped $(docker stop $container_name)"
else
echo "Removing existing container..."
fi
echo "Removed $(docker rm $container_name)"
docker rmi $(docker images -q $image_name)
fi
fi
# Run Docker container in the background and capture container ID
2024-01-05 02:26:21 +00:00
docker build --no-cache -t $image_name .
2024-01-05 00:29:42 +00:00
if [ $deploy ]; then
container_id=$(docker run --name $container_name -p 3000:3000 -d $image_name)
echo "Container deployed: $container_id"
elif [ !$follow ]; then
exit 0
fi
# Function to display a message after interrupt
function exit_handler() {
# Clear the current line
echo -e "\033[2K"
if [ $stay ]; then
echo "Container is still running. To stop it, use:"
echo "docker stop $container_id"
else
echo "Stopping the container ..."
docker stop $container_id
echo "Stopped."
fi
}
# Register the exit_handler function to be called when the script receives the INT signal (Ctrl+C)
trap exit_handler INT
# Run Docker logs on the specified container
docker logs -f $container_id