简体   繁体   English

如何在docker-compose中修复Jedis中的拒绝连接错误?

[英]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 . 当redis在docker-compose内部运行时,我正在使用Redis服务器和Java应用程序设置docker-compose,我的代码可以与HOST OS中的外部Java程序一起正常工作。

but when i put java program inside container i am getting 但是当我将Java程序放入容器中时
java.net.ConnectException: Connection refused java.net.ConnectException:连接被拒绝

Redis-cli 127.0.0.1:6379> is also working fine Redis-cli 127.0.0.1:6379>也可以正常工作


        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 当使用“ redis”而不是“ localhost”时,它从docker-compose外部给我未知主机错误,并且在docker-compose内拒绝连接

this is my dockercompose.yml 这是我的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. 尝试显式指定网络并添加depends_on以确保在工作人员需要时redis已启动。

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); 使用new Jedis("redis", 6379);创建jedis实例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. 这是一个没有docker compose 的网络配置示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在通过 docker-compose 启动 rabbit 时出现连接拒绝错误? - Why I have a connection refused error when starting rabbit by docker-compose? 使用docker-compose启动dockerized Spring Boot和MariaDb时出现连接拒绝错误 - Connection refused error when starting dockerized Spring Boot and MariaDb with docker-compose Docker-组成up springboot + postgres连接被拒绝 - Docker-compose up springboot + postgres Connection refused Spring Boot + docker-compose + MySQL:连接被拒绝 - Spring Boot + docker-compose + MySQL: Connection refused Jedis连接被拒绝 - Jedis connection refused 可怕的 Java SpringBoot 应用程序未通过 Docker-compose java.net.ConnectException 连接到 MySQL:连接被拒绝 - The dreaded Java SpringBoot app not connecting to MySQL with Docker-compose java.net.ConnectException: Connection refused docker-compose java应用程序连接到mongodb - docker-compose java application connection to mongodb Docker-compose 容器连接到 MySql 不起作用 - Docker-compose container connection to MySql not working docker-compose:容器之间没有连接 - docker-compose: no connection between containers 从Selenium RemoteWebDriver与Selenium Docker Firefox容器连接时,如何解决“拒绝连接”错误? - How to fix “Connection Refused” error while connecting with Selenium Docker Firefox container from Selenium RemoteWebDriver?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM