0

I'm new to Docker. I'm build a Spring Boot Application deploying on Docker.

Here is my example docker-compose.yml:

version: "3"

services:

  user:
    container_name: user
    image: user-service
    build:
      context: user-api/
    ports:
      - "3001:8000"
    restart: always
    volumes: 
      - /home/ubuntu/logs/user_service:/opt/app/logs
    networks:
      - api_network

  cms:
    container_name: cms
    image:cms-service
    build:
      context: cms-service/
    ports:
      - "3003:8000"
    restart: always  
    volumes: 
      - /home/ubuntu/logs/cms_service:/opt/app/logs
    networks:
      - api_network

networks:
  api_network:
    driver: bridge

In the server machine, there's a Redis Server running on Ubuntu. I cannot connect the the Redis Server from Docker container to the host machine.

Here is my redis config inside application.properties

spring.redis.host=localhost
spring.redis.port=6379
spring.redis.password=Password123!@#

I also tried to change the localhost to

  • 127.0.0.1
  • 172.17.0.1
  • 0.0.0.0
  • host
  • host.docker.internal

That's I've found on the internet. But nothing works. Do I need to specifically config anything to allow my Spring Boot Service inside Docker connect to Redis that's running on localhost.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Hikaru Shindo
  • 2,611
  • 8
  • 34
  • 59
  • https://stackoverflow.com/questions/48546124/what-is-linux-equivalent-of-host-docker-internal – Роман Сергеевич Jul 23 '21 at 11:13
  • @РоманСергеевич extra_hosts: - "host.docker.internal:host-gateway" Is not working for me. – Hikaru Shindo Jul 23 '21 at 11:21
  • Try to add extra_hosts: - "localhost:127.0.0.1" inside docker-compose is still not working. – Hikaru Shindo Jul 23 '21 at 11:39
  • A Redis server is pretty lightweight; can you add a Redis container to your `docker-compose.yml`, instead of trying to reach out of Docker space? `localhost` in Docker generally means "this container" (and so in this question there are three different context-specific meanings of `localhost`). – David Maze Jul 23 '21 at 12:57
  • @DavidMaze Thanks for the suggestion. I will talk to the Infra team about this. – Hikaru Shindo Jul 24 '21 at 14:21

1 Answers1

1

The issue is probably due to the fact your Redis is bound to the address 127.0.0.1 (which is the default configuration) and your containers are not running on the network host.

To solve this, you should reconfigure Redis to bind to both 127.0.0.1 as well as to the IP address of the host as seen from api_network (sudo ip addr show on the host): the easiest thing to do here, if your scenario allows that, is to just bind Redis to 0.0.0.0 (via redis.conf).

As an alternative, you may also want to run your containers on the host network instead of using the api_network bridge: this appears to be overkill according to your issue, by the way, and may lead to security issues and exposed network ports.

Efran Cobisi
  • 6,138
  • 22
  • 22