Describe the bug
Unable to connect Redis to Spring boot dockerize application
To reproduce
I'm using a Redis image for my Spring Boot application and its running perfectly in my local. However in docker-compose environment, although I have followed every instruction, document, tried every configuration, I still cannot connect it to my Dockerize Spring Boot application.
docker-compose.yml:
version: '3.8'
services:
app:
container_name: mikabackend
image:
environment:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/postgres
SPRING_DATASOURCE_USERNAME: username
SPRING_DATASOURCE_PASSWORD: password
SPRING_REDIS_HOST: redis
SPRING_REDIS_PORT: 6379
JWT_SECRET_KEY: ${JWT_SECRET_KEY}
MAIL_USERNAME: ${MAIL_USERNAME}
MAIL_PASSWORD: ${MAIL_PASSWORD}
ports:
- "8080:8080"
depends_on:
redis:
condition: service_healthy
postgres:
condition: service_started
networks:
- mika
restart: unless-stopped
postgres:
container_name: postgres-sql
image: postgres
environment:
POSTGRES_USER: username
POSTGRES_PASSWORD: password
PGDATA: /data/postgres
volumes:
- postgres:/data/postgres
ports:
- "5432:5432"
networks:
- mika
restart: unless-stopped
pgadmin:
container_name: pgadmin
image: dpage/pgadmin4
environment:
PGADMIN_DEFAULT_EMAIL: ${PGADMIN_DEFAULT_EMAIL:-pgadmin4@pgadmin.org}
PGADMIN_DEFAULT_PASSWORD: ${PGADMIN_DEFAULT_PASSWORD:-admin}
PGADMIN_CONFIG_SERVER_MODE: 'False'
volumes:
- pgadmin:/var/lib/pgadmin
ports:
- "5050:80"
networks:
- mika
depends_on:
- postgres
restart: unless-stopped
redis:
image: redis:7-alpine
container_name: redis
ports:
- "6379:6379"
volumes:
- redis_data:/data
networks:
- mika
healthcheck:
test: [ "CMD", "redis-cli", "ping" ]
interval: 1s
timeout: 2s
retries: 10
networks:
mika:
driver: bridge
volumes:
postgres:
pgadmin:
redis_data:
application.yml
spring:
datasource:
url: jdbc:postgresql://localhost:5432/postgres
username: username
password: password
driver-class-name: org.postgresql.Driver
jpa:
hibernate:
ddl-auto: update
show-sql: false
properties:
hibernate:
format_sql: true
database: postgresql
application:
name: mika
mail:
host: smtp.gmail.com
port: 587
username: ${MAIL_USERNAME}
password: ${MAIL_PASSWORD}
properties:
mail:
smtp:
auth: true
starttls:
enable: true
data:
redis:
host: redis
port: 6379
application:
security:
jwt:
secret-key: ${JWT_SECRET_KEY}
expiration: 86400000 # a day
refresh-token:
expiration: 604800000 # 7 days
server:
port: 8080
app:
password:
default: 12345678
management:
health:
mail:
enabled: off
file:
storage:
path: /uploads
application log
2024-11-03 22:49:34 mikabackend |
2024-11-03 22:49:34 mikabackend | . ____ _ __ _ _
2024-11-03 22:49:34 mikabackend | /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
2024-11-03 22:49:34 mikabackend | ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
2024-11-03 22:49:34 mikabackend | \\/ ___)| |_)| | | | | || (_| | ) ) ) )
2024-11-03 22:49:34 mikabackend | ' |____| .__|_| |_|_| |_\__, | / / / /
2024-11-03 22:49:34 mikabackend | =========|_|==============|___/=/_/_/_/
2024-11-03 22:49:34 mikabackend | :: Spring Boot :: (v3.2.3)
2024-11-03 22:49:34 mikabackend |
2024-11-03 22:49:34 mikabackend | 2024-11-03 15:49:34 INFO o.h.validator.internal.util.Version - HV000001: Hibernate Validator 8.0.1.Final
2024-11-03 22:49:34 mikabackend | 2024-11-03 15:49:34 INFO c.m.a.AnimestyleBackendApplication - Starting AnimestyleBackendApplication v0.0.1-SNAPSHOT using Java 17-ea with PID 1 (/app/app.jar started by root in /app)
2024-11-03 22:49:34 mikabackend | 2024-11-03 15:49:34 INFO c.m.a.AnimestyleBackendApplication - No active profile set, falling back to 1 default profile: "default"
2024-11-03 22:49:36 mikabackend | 2024-11-03 15:49:36 INFO o.s.d.r.c.RepositoryConfigurationDelegate - Multiple Spring Data modules found, entering strict repository configuration mode
2024-11-03 22:49:36 mikabackend | 2024-11-03 15:49:36 INFO o.s.d.r.c.RepositoryConfigurationDelegate - Bootstrapping Spring Data JPA repositories in DEFAULT mode.
2024-11-03 22:49:36 mikabackend | 2024-11-03 15:49:36 INFO o.s.d.r.c.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 160 ms. Found 14 JPA repository interfaces.
2024-11-03 22:49:36 mikabackend | 2024-11-03 15:49:36 INFO o.s.d.r.c.RepositoryConfigurationDelegate - Multiple Spring Data modules found, entering strict repository configuration mode
2024-11-03 22:49:36 mikabackend | 2024-11-03 15:49:36 INFO o.s.d.r.c.RepositoryConfigurationDelegate - Bootstrapping Spring Data Redis repositories in DEFAULT mode.
2024-11-03 22:49:36 mikabackend | 2024-11-03 15:49:36 INFO o.s.d.r.c.RepositoryConfigurationDelegate - Finished Spring Data repository scanning in 6 ms. Found 1 Redis repository interface.
2024-11-03 22:49:37 mikabackend | 2024-11-03 15:49:37 INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port 8080 (http)
2024-11-03 22:49:37 mikabackend | 2024-11-03 15:49:37 INFO o.a.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8080"]
2024-11-03 22:49:37 mikabackend | 2024-11-03 15:49:37 INFO o.a.catalina.core.StandardService - Starting service [Tomcat]
2024-11-03 22:49:37 mikabackend | 2024-11-03 15:49:37 INFO o.a.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/10.1.19]
2024-11-03 22:49:38 mikabackend | 2024-11-03 15:49:38 INFO o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext
2024-11-03 22:49:38 mikabackend | 2024-11-03 15:49:38 INFO o.s.b.w.s.c.ServletWebServerApplicationContext - Root WebApplicationContext: initialization completed in 3142 ms
2024-11-03 22:49:38 mikabackend | 2024-11-03 15:49:38 INFO o.h.jpa.internal.util.LogHelper - HHH000204: Processing PersistenceUnitInfo [name: default]
2024-11-03 22:49:38 mikabackend | 2024-11-03 15:49:38 INFO org.hibernate.Version - HHH000412: Hibernate ORM core version 6.4.4.Final
2024-11-03 22:49:38 mikabackend | 2024-11-03 15:49:38 INFO o.h.c.i.RegionFactoryInitiator - HHH000026: Second-level cache disabled
2024-11-03 22:49:39 mikabackend | 2024-11-03 15:49:39 INFO o.s.o.j.p.SpringPersistenceUnitInfo - No LoadTimeWeaver setup: ignoring JPA class transformer
2024-11-03 22:49:39 mikabackend | 2024-11-03 15:49:39 INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
2024-11-03 22:49:39 mikabackend | 2024-11-03 15:49:39 INFO com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Added connection org.postgresql.jdbc.PgConnection@33060020
2024-11-03 22:49:39 mikabackend | 2024-11-03 15:49:39 INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
2024-11-03 22:49:39 mikabackend | 2024-11-03 15:49:39 WARN org.hibernate.orm.deprecation - HHH90000025: PostgreSQLDialect does not need to be specified explicitly using 'hibernate.dialect' (remove the property setting and it will be selected by default)
2024-11-03 22:49:40 mikabackend | 2024-11-03 15:49:40 INFO o.h.e.t.j.p.i.JtaPlatformInitiator - HHH000489: No JTA platform available (set 'hibernate.transaction.jta.platform' to enable JTA platform integration)
2024-11-03 22:49:40 mikabackend | 2024-11-03 15:49:40 INFO o.s.o.j.LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'default'
2024-11-03 22:49:41 mikabackend | 2024-11-03 15:49:41 INFO o.s.d.j.r.query.QueryEnhancerFactory - Hibernate is in classpath; If applicable, HQL parser will be used.
2024-11-03 22:49:43 mikabackend | 2024-11-03 15:49:43 WARN o.s.b.a.o.j.JpaBaseConfiguration$JpaWebConfiguration - spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
2024-11-03 22:49:43 mikabackend | 2024-11-03 15:49:43 INFO o.s.b.a.e.web.EndpointLinksResolver - Exposing 1 endpoint(s) beneath base path '/actuator'
2024-11-03 22:49:43 mikabackend | 2024-11-03 15:49:43 INFO o.s.s.web.DefaultSecurityFilterChain - Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@42e58f8e, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@2f815a12, org.springframework.security.web.context.SecurityContextHolderFilter@b625b00, org.springframework.security.web.header.HeaderWriterFilter@5a27a096, org.springframework.web.filter.CorsFilter@1d67a1ad, org.springframework.security.web.authentication.logout.LogoutFilter@7e9e125f, com.miki.animestylebackend.config.JwtAuthenticationFilter@f5d20d8, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@5e0fbfb6, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@53279691, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@4aa0b026, org.springframework.security.web.session.SessionManagementFilter@7447b08c, org.springframework.security.web.access.ExceptionTranslationFilter@a11811c, org.springframework.security.web.access.intercept.AuthorizationFilter@44eb9af1]
2024-11-03 22:49:44 mikabackend | 2024-11-03 15:49:44 INFO o.a.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8080"]
2024-11-03 22:49:44 mikabackend | 2024-11-03 15:49:44 INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat started on port 8080 (http) with context path ''
2024-11-03 22:49:44 mikabackend | 2024-11-03 15:49:44 INFO c.m.a.AnimestyleBackendApplication - Started AnimestyleBackendApplication in 10.657 seconds (process running for 11.341)
2024-11-03 22:50:49 mikabackend | 2024-11-03 15:50:49 INFO o.a.c.c.C.[Tomcat].[localhost].[/] - Initializing Spring DispatcherServlet 'dispatcherServlet'
2024-11-03 22:50:49 mikabackend | 2024-11-03 15:50:49 INFO o.s.web.servlet.DispatcherServlet - Initializing Servlet 'dispatcherServlet'
2024-11-03 22:50:49 mikabackend | 2024-11-03 15:50:49 INFO o.s.web.servlet.DispatcherServlet - Completed initialization in 3 ms
2024-11-03 22:50:49 mikabackend | 2024-11-03 15:50:49 INFO c.m.a.controller.BaseController - Current user: admin@gmail.com
2024-11-03 22:50:49 mikabackend | 2024-11-03 15:50:49 INFO c.m.a.controller.BaseController - Current user: admin@gmail.com
Redis log
2024-11-03 22:42:58 1:C 03 Nov 2024 15:42:58.650 # WARNING Memory overcommit must be enabled! Without it, a background save or replication may fail under low memory condition. Being disabled, it can also cause failures without low memory condition, see https://github.com/jemalloc/jemalloc/issues/1328. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
2024-11-03 22:42:58 1:C 03 Nov 2024 15:42:58.650 * oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
2024-11-03 22:42:58 1:C 03 Nov 2024 15:42:58.650 * Redis version=7.4.1, bits=64, commit=00000000, modified=0, pid=1, just started
2024-11-03 22:42:58 1:C 03 Nov 2024 15:42:58.650 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
2024-11-03 22:42:58 1:M 03 Nov 2024 15:42:58.650 * monotonic clock: POSIX clock_gettime
2024-11-03 22:42:58 1:M 03 Nov 2024 15:42:58.651 * Running mode=standalone, port=6379.
2024-11-03 22:42:58 1:M 03 Nov 2024 15:42:58.651 * Server initialized
2024-11-03 22:42:58 1:M 03 Nov 2024 15:42:58.651 * Loading RDB produced by version 7.4.1
2024-11-03 22:42:58 1:M 03 Nov 2024 15:42:58.651 * RDB age 179 seconds
2024-11-03 22:42:58 1:M 03 Nov 2024 15:42:58.651 * RDB memory usage when created 0.93 Mb
2024-11-03 22:42:58 1:M 03 Nov 2024 15:42:58.651 * Done loading RDB, keys loaded: 0, keys expired: 0.
2024-11-03 22:42:58 1:M 03 Nov 2024 15:42:58.652 * DB loaded from disk: 0.000 seconds
2024-11-03 22:42:58 1:M 03 Nov 2024 15:42:58.652 * Ready to accept connections TCP
Testing API:
Expected behavior
Redis can connect to spring boot dockerize application
Comment From: sundb
@diegok what's the config in your spring app.
and you can first use redis-cli to check if you can connect Redis, if it works, then you need to make sure the spring config is correct.
Comment From: dvcminh
Hi, thank you for your reply. The config in my spring app is the application.yml I have sent above I do use redis-cli to ensure redis is healthy before my spring app can connect with it (you can see it in the docker-compose.yml file)
Comment From: iyahimovitz
Sounds like a docker DNS cache problem to me since you're trying to connect using redis but your health check is using localhost. I would try to telnet to redis using theredis hostname from app container and see if it connects.
Also, did you try to recreate the containers? sometimes that does help with DNS cache issues.