Skip to content

Docker-compose - Redis-Springboot #38774

Closed as not planned
Closed as not planned
@VidyasagarPandikashala

Description

@VidyasagarPandikashala

#About application

Simple backend applicaction that counts number of visits happened to be done by an user.
Redis stores the data of the visits made.


ERROR DESCRIPTION

Springboot application is not able connect to the redis server even though it shows the redis and application are both in same docker network.

It throws error - White label Error (type=Internal Server Error, status=500).

Error in sprinboot-app container:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception 
[Request processing failed: 
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis] with root cause

Dockerfile:

FROM openjdk:17
WORKDIR /app
COPY target/visitsapplication-0.0.1-SNAPSHOT.jar app.jar
EXPOSE 8080
CMD ["java", "-jar", "app.jar"]

DockerCompose.yml

version: '3'
services:
    redis-server:
    image: 'redis'
    ports:
        - "6379:6379"
    springboot-app:
    build: .
    ports:
        - "8080:8080"  

application.properties

server.port=8080
spring.redis.host=redis-server
spring.redis.port=6379

VisitController.java

package com.visitapplication.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/")
public class VisitsController {
        @Autowired
        private RedisTemplate<String, String> redisTemplate;

        @GetMapping
        public String getVisits() {
            ValueOperations<String, String> ops = redisTemplate.opsForValue();
            String visits = ops.get("visits");

            if (visits == null) {
                visits = "0";
            }

            ops.set("visits", String.valueOf(Integer.parseInt(visits) + 1));

            return "Number of visits is " + visits;
        }
}

Commands run to check the issues


Command:
 docker-compose logs springboot-app
Output :
java.net.ConnectException: Connection refused.

Command:
docker network inspect visitsapplication_default
Output:
[
    {
        "Name": "visitsapplication_default",
        "Id": "671f39cacd25f8f2c694eebed7515c16dfae3efedc86ae57c567e8ab0567fed2",
        "Created": "2023-12-14T06:06:20.465060571Z",
        "Scope": "local",
        "Driver": "bridge",
        "EnableIPv6": false,
        "IPAM": {
            "Driver": "default",
            "Options": null,
            "Config": [
                {
                    "Subnet": "172.20.0.0/16",
                    "Gateway": "172.20.0.1"
                }
            ]
        },
        "Internal": false,
        "Attachable": false,
        "Ingress": false,
        "ConfigFrom": {
            "Network": ""
        },
        "ConfigOnly": false,
        "Containers": {
            "7437823988de4a68b31f72b423d75bd7172be555b7c77879a5f76eb3cb248d1d": {
                "Name": "visitsapplication-redis-server-1",
                "EndpointID": "c138d3fa863efed0accf193acc4992a382245e160840337554fc76f74e3f914e",
                "MacAddress": "02:42:ac:14:00:03",
                "IPv4Address": "172.20.0.3/16",
                "IPv6Address": ""
            },
            "be2ef8d7f9ed05fb7b98e21473948b7f005fa0fb3b268f03105a98ee5ae79eeb": {
                "Name": "visitsapplication-springboot-app-1",
                "EndpointID": "fa1ee3bccb4097dbe8cfb5388eb471c9836e2a84b21e94fc266d3542e0e81e5a",
                "MacAddress": "02:42:ac:14:00:02",
                "IPv4Address": "172.20.0.2/16",
                "IPv6Address": ""
            }
        },
        "Options": {},
        "Labels": {
            "com.docker.compose.network": "default",
            "com.docker.compose.project": "visitsapplication",
            "com.docker.compose.version": "2.17.3"
        }
    }
]

The Docker network inspection confirms that both visitsapplication-redis-server-1 and visitsapplication-springboot-app-1 containers are on the same Docker network named visitsapplication_default. Therefore, they should be able to communicate with each other using their service names (redis-server and springboot-app, respectively).


Metadata

Metadata

Assignees

No one assigned

    Labels

    for: stackoverflowA question that's better suited to stackoverflow.com

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions