简体   繁体   中英

Unable to connect from Spring Boot to Dockerized Redis in outside/inside machine

I am connecting to Redis from the spring boot app on the outside machine where the Redis server docker container is not ruSo wheng. When the app tries to connect to Redis, the app can't cappropriatelyroperly until the sent request is timed out. Meanwhile, if I try to connect from:

  1. Inside the machine where the Redis server docker container is running with the host is localhost, I could connect it. And, I don't know why I can't connect by setup host value as a numerical IP/alphabetical (URL), only works with "localhost."
  2. Outside machine where the Redis server docker container is not running with Redis client app GUI for management, I could connect it.

application.properties:

spring.redis.host=pc-1
spring.redis.port=6379

pc-1 is alias from some numerical ip. I'am using hosts feature from windows to aliasing/redirecting it.

.env:

REDIS_PORT=6379

docker-compose.yml:

  redis:
    image: redis:latest
    ports:
      - "${REDIS_PORT}:6379"
    command:
      # - redis-server
      # - --requirepass "${REDIS_PASSWORD}"
    networks:
      - redis
    healthcheck:
      test: ["CMD-SHELL", "redis-cli ping"]
      interval: 10s
      timeout: 10s
      retries: 3

I need help on this issue.

Use the --service-ports flag to the docker compose command to publish the ports you've defined in the docker compose file.

Other debugging tips:

  • Hardcode the ${REDIS_PORT} variable in case the value is not getting set or set a default like ${REDIS_PORT:-default}
  • Pass the env file explicitly like docker compose --env-file./somedir/.env up in case the env file is not being pick up
  • Use docker inspect to get container status, check the networking info

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM