Skip to content

Instantly share code, notes, and snippets.

@IdrisAkintobi
Last active March 26, 2025 10:56
Show Gist options
  • Save IdrisAkintobi/0628e9545df491aa9cfb8bc4f49933ce to your computer and use it in GitHub Desktop.
Save IdrisAkintobi/0628e9545df491aa9cfb8bc4f49933ce to your computer and use it in GitHub Desktop.
Redis-Docker
# Use the official Redis image from the Docker Hub
FROM redis:7.4
# Copy the custom Redis configuration file
COPY redis.conf /usr/local/etc/redis/redis.conf
# Expose the default Redis port
EXPOSE 6379
# Start Redis server with the custom configuration
CMD ["redis-server", "/usr/local/etc/redis/redis.conf"]
# Use the below command to use with password.
# CMD ["sh", "-c", "redis-server /usr/local/etc/redis/redis.conf --requirepass ${REDIS_PASSWORD}"]
# Variables
IMAGE_NAME = my-redis
CONTAINER_NAME = my-redis-container
PORT = 6379
VOLUME_NAME = redis-data
CONFIG_FILE = redis.conf
# Build the Docker image
build:
docker build -t $(IMAGE_NAME) .
# Run the Redis container with a password prompt
run:
docker run -d \
--name $(CONTAINER_NAME) \
-p $(PORT):$(PORT) \
-v $(VOLUME_NAME):/data \
-v $(PWD)/$(CONFIG_FILE):/usr/local/etc/redis/redis.conf \
$(IMAGE_NAME)
# Use the below command to use with password
# run:
# @echo "Enter Redis password:"
# @read -s REDIS_PASSWORD; \
# docker run -d \
# --name $(CONTAINER_NAME) \
# -p $(PORT):$(PORT) \
# -v $(VOLUME_NAME):/data \
# -v $(PWD)/$(CONFIG_FILE):/usr/local/etc/redis/redis.conf \
# -e REDIS_PASSWORD="$$REDIS_PASSWORD" \
# $(IMAGE_NAME)
# Connect to the Redis server using redis-cli
connect:
docker exec -it $(CONTAINER_NAME) redis-cli
# Start the Redis container
start:
docker start $(CONTAINER_NAME)
# Stop the Redis container
stop:
docker stop $(CONTAINER_NAME)
# Remove the Redis container and associated volume
clean:
docker rm -f $(CONTAINER_NAME)
docker volume rm $(VOLUME_NAME)
# Show logs from the Redis container
logs:
docker logs $(CONTAINER_NAME)
# List all Docker containers
ps:
docker ps -a
# Help command to display available targets
help:
@echo "Available targets:"
@echo " build - Build the Docker image"
@echo " run - Run the Redis container (prompts for password)"
@echo " connect - Connect to the Redis server using redis-cli"
@echo " start - Start the Redis container"
@echo " stop - Stop the Redis container"
@echo " clean - Remove the Redis container and associated volume"
@echo " logs - Show logs from the Redis container"
@echo " ps - List all Docker containers"
@echo " help - Display this help message"
# Bind Redis to all interfaces
bind 0.0.0.0
# Enable persistence
save 900 1
save 300 10
save 60 10000
# Append-only file (AOF) persistence
appendonly yes
appendfilename "appendonly.aof"
# Set the working directory for Redis data
dir /data
# Log level (options: debug, verbose, notice, warning)
loglevel notice
# Log file (optional)
logfile ""
@IdrisAkintobi
Copy link
Author

Redis Docker Setup

This gist contains a Dockerized setup for running a Redis server with custom configuration and optional password authentication. The setup includes a Dockerfile, redis.conf, and a Makefile for easy management of the Redis container. Save all files to a repository an use the make file to run the commands. run make help to see the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment