1. 문제 상황

서버로 요청을 보낼 때 다음과 같은 문제가 발생함

Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost/127.0.0.1:6379

2. 문제 원인 탐색

아무래도 application.yml에서 redis 관련 설정을 만져야 할 것 같았음

spring:
  data:
    redis:
      host: localhost
      port: 6379

Redis는 EC2에서 sudo service redis-server start로 실행했었고

ubuntu@ip-172-31-6-104:~$ sudo service redis-server status
● redis-server.service - Advanced key-value store
     Loaded: loaded (/lib/systemd/system/redis-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2024-11-21 04:36:56 UTC; 1 week 0 days ago
       Docs: <http://redis.io/documentation>,
             man:redis-server(1)
   Main PID: 5300 (redis-server)
     Status: "Ready to accept connections"
      Tasks: 5 (limit: 1130)
     Memory: 3.0M
        CPU: 12min 35.490s
     CGroup: /system.slice/redis-server.service
             └─5300 "/usr/bin/redis-server 127.0.0.1:6379" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Nov 21 04:36:56 ip-172-31-6-104 systemd[1]: Starting Advanced key-value store...
Nov 21 04:36:56 ip-172-31-6-104 systemd[1]: Started Advanced key-value store.

status 명령어로 정상 동작 중임을 확인

localhost는 docker container 내부에서만 유효하기 때문에 바깥에서 실행 중인 redis와 연결해줄 방법을 찾아야 했음

그리하여 application.yml을 다음과 같이 수정

spring:
  data:
    redis:
      host: host.docker.internal
      port: 6379

하지만 여전히 redis에 연결할 수 없었음

Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to host.docker.internal:6379
...중략...
Caused by: java.net.UnknownHostException: host.docker.internal: Name or service not known