Last active
May 3, 2021 10:49
-
-
Save jkomyno/c72248f2baf719b328a735494c62e944 to your computer and use it in GitHub Desktop.
Run Redis in Docker and monitor every Redis command received in real-time
This file contains hidden or 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 | |
# Usage: ./redis_dev.sh | |
# Description: spawn a Redis instance in a background Docker container, and show the logs | |
# of each Redis command received in real-time. Hitting Ctrl+C will terminate the process and | |
# kill the Docker container used by the script. | |
set -e | |
# Edit this variables accordingly to your use case | |
DOCKER_IMAGE="redis:6.2.2-alpine" | |
HOST_PORT=6379 | |
CONTAINER_PORT=6379 | |
REDIS_PASSWORD="" | |
# Run Redis on a background Docker instance, and expose it on localhost:${HOST_PORT} | |
echo "Preparing to run Redis on localhost:${HOST_PORT}..." | |
CONTAINER_ID=$(docker run -p ${HOST_PORT}:${CONTAINER_PORT} -d ${DOCKER_IMAGE}) | |
echo "Created Docker container ${CONTAINER_ID}" | |
echo "Redis is running on localhost:${HOST_PORT}" | |
# on Ctrl+C, call unmount() | |
trap unmount SIGINT | |
# Unmount the docker image of | |
function unmount() { | |
# Stop the Redis container spawned previously | |
echo "" | |
echo "Closing Docker container ${CONTAINER_ID}..." | |
docker stop "${CONTAINER_ID}" > /dev/null | |
} | |
# Login into Redis in a background process and log the commands sent to it in real-time | |
echo "Attaching to redis-cli on localhost:${HOST_PORT}..." | |
docker exec -t "${CONTAINER_ID}" redis-cli \ | |
--no-auth-warning -a "${REDIS_PASSWORD}" \ | |
MONITOR & | |
echo "Attached to redis-cli on localhost:${HOST_PORT}" | |
# Save the PID of the of the background Redis logger process | |
CONT_PID=$! | |
# Show the stdout of the background Redis logger process in real-time | |
cat /proc/${CONT_PID}/fd/1 | |
wait ${CONT_PID} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment