简体   繁体   中英

How to fix connection refused error in Jedis in docker-compose?

i am setting docker-compose with redis server and java application, when redis run inside docker-compose my code working fine with outside java program in HOST OS .

but when i put java program inside container i am getting
java.net.ConnectException: Connection refused

Redis-cli 127.0.0.1:6379> is also working fine


        try {
            Thread.sleep(3000);

            jedis = new Jedis("localhost");
            this.restTemplate = new RestTemplate(); 
            startWorking();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

when use "redis" instead of "localhost" its give me unknown host error from outside docker-compose and connection refused inside docker-compose

this is my dockercompose.yml

 redis:
    image: redis
    ports:
    - "6379:6379"

  worker:
    build: worker

Try specifying networks explicitly and adding depends_on to make sure redis is up when the worker needs it.

version: "3.7"
services:

  worker:
    build: worker
    networks:
      - net
    depends_on:
      - redis

  redis:
    image: redis
    ports:
      - "6379:6379"
    networks:
      - net

networks:
  net:

Create the jedis instance with new Jedis("redis", 6379); .

You might want to read more about networking in compose and dependencies between services .

You can also try running an example without compose and then transforming it to a compose configuration. Here is an example of network configuration without docker compose.

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