MrJazsohanisharma

Docker with Redis And Spring Boot

Blog ads

Docker with Redis: Set Up Redis in a Docker Container and Connect It with Spring Boot

Running Redis in a Docker container is a modern, efficient way to add high-performance caching and data storage to your Spring Boot applications. Docker makes it easy to deploy, manage, and scale Redis in any environment, while Spring Boot’s integration with Redis allows for seamless connectivity and rapid development. In this guide, you’ll learn how to set up Redis using Docker and connect it to a Spring Boot application, following best practices for security and data persistence.

Why Use Docker for Redis?

  • Portability: Run Redis anywhere Docker is supported, from local development to production servers.
  • Isolation: Avoid conflicts with other services by running Redis in its own containerized environment.
  • Easy Scaling: Quickly spin up, scale, or replace Redis instances as needed.
  • Persistence: Mount volumes to ensure your data survives container restarts.

Step 1: Pull the Official Redis Docker Image

docker pull redis:latest

This command downloads the latest Redis image from Docker Hub.
You can specify a version if needed (e.g., redis:7.0).

Step 2: Create a Directory for Data Persistence

mkdir -p ~/redis_data

This ensures Redis data is stored on the host and survives container restarts or removals.[8]

Step 3: Run the Redis Container

docker run -d \
  --name redis \
  -p 6379:6379 \
  -v ~/redis_data:/data \
  --restart always \
  redis:latest \
  redis-server --requirepass your_strong_redis_password

  • -d: Detached mode
  • --name redis: Names the container "redis"
  • -p 6379:6379: Maps Redis port to the host
  • -v ~/redis_data:/data: Mounts host directory for data persistence
  • --restart always: Ensures Redis restarts automatically
  • --requirepass: Sets a password for Redis security

Check the container status with docker ps and connect to Redis using:

docker exec -it redis redis-cli -a your_strong_redis_password

Step 4: Configure Spring Boot to Connect to Redis

Add the following to your application.properties (or application.yml):

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=your_strong_redis_password
spring.redis.timeout=2000

Spring Boot will auto-configure the connection using these properties.[6][8]

Step 5: Add Spring Data Redis Dependency

<dependency>
  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
  &lt;artifactId&gt;spring-boot-starter-data-redis&lt;/artifactId&gt;
</dependency>

Step 6: Example Redis Service in Spring Boot

@Service
public class RedisExampleService {
    @Autowired
    private StringRedisTemplate redisTemplate;

    public void save(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }
}

Now you can use RedisExampleService in your controllers or business logic to interact with Redis.

Step 7: Test the Integration

  1. Start your Redis container using Docker.
  2. Start your Spring Boot application.
  3. Use your API or a test controller to save and retrieve data via Redis.
  4. Verify data persistence and connectivity.

Best Practices

  • Always use strong passwords for production Redis instances.
  • Map volumes for data persistence to avoid data loss.
  • Restrict network access to the Redis port in production environments.
  • Use environment variables or secrets management for sensitive configuration.

Conclusion

Running Redis in a Docker container and connecting it to your Spring Boot application is straightforward and production-ready. This approach gives you flexibility, portability, and robust data management for modern cloud-native applications.

ads

Previous Post Next Post