Created
December 21, 2024 17:00
-
-
Save samarpanda/194c026d910083907bb831f3eba44de5 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
# Configuration | |
ENV=local | |
PROJECT=demo | |
IMAGE=redis:7.4.0 | |
CNAME=redis-$ENV-$PROJECT | |
PORT=6379 | |
DATA_DIR=$PWD/data | |
# Helper function to display usage | |
usage() { | |
echo "Usage: $0 {start|stop|restart}" | |
exit 1 | |
} | |
# Start Redis container | |
start_redis() { | |
echo "Starting Redis container: $CNAME" | |
docker pull $IMAGE | |
docker stop --time=20 $CNAME 2>/dev/null || true | |
docker rm -f $CNAME 2>/dev/null || true | |
if [ ! -d "$DATA_DIR" ]; then | |
mkdir -p $DATA_DIR | |
echo "Created data directory: $DATA_DIR" | |
fi | |
docker run -d \ | |
--name $CNAME \ | |
-p $PORT:6379 \ | |
-v $DATA_DIR:/data \ | |
$IMAGE redis-server --appendonly yes | |
echo "Redis container started on port $PORT" | |
} | |
# Stop Redis container | |
stop_redis() { | |
echo "Stopping Redis container: $CNAME" | |
docker stop --time=20 $CNAME | |
docker rm -f $CNAME | |
echo "Redis container stopped and removed" | |
} | |
# Restart Redis container | |
restart_redis() { | |
echo "Restarting Redis container: $CNAME" | |
stop_redis | |
start_redis | |
} | |
# Main script logic | |
if [ "$#" -ne 1 ]; then | |
usage | |
fi | |
case "$1" in | |
start) | |
start_redis | |
;; | |
stop) | |
stop_redis | |
;; | |
restart) | |
restart_redis | |
;; | |
*) | |
usage | |
;; | |
esac | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment