Search code examples
spring-bootredisjedisdocker-image

Spring Boot Dockerize project and redis container getting java.net.ConnectException: Connection refused (Connection refused)


I am working with a Dockerize spring boot application and redis in another container. I run the redis container in this way: docker run -d --name redis -p 6379:6379 redis

When I run my application from the id I don't have problems and my application is able to connect to redis.

But when I run my app as a container: docker run -p 8080:8080 shortenurl, I'm getting the next issue: java.net.ConnectException: Connection refused (Connection refused).

This is my pom.xml:

<groupId>com.neueda.shorturl</groupId>
    <artifactId>shortenurl</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>shortenurl</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <finalName>shortenurl</finalName>
    </build>

And this is my application.yml configuration:

spring:
  application:
    name: shortenurl-neueda-assignment
  redis:
    host: localhost
    port: 6379

Any ideas?

Thanks.


Solution

  • That because, once you run your applications in two different containers they are not in the same network anymore. You can't access localhost or any loop back IP unless you are in the container. Inside the container only localhost. But you can access the containers by their container name, if both of the containers are in same network.

    Therefore, you should orchestrate a docker network and deploy both of the containers in that network, then each other container able to resolve hosts by their container name.

    Try this;

    $ docker network create test-netw
    $ docker run --net test-netw -d --name redis -p 6379:6379 redis
    

    And change Redis host in aplication.yml file;

    spring:
      redis:
        host: redis
        port: 6379
        jedis:
          pool:
            max-active: 7
            max-idle: 7
            min-idle: 2
    

    Add this configuration as well;

    @Bean
    JedisPool jedisPool(RedisProperties redisProperties) {
        return new JedisPool(new JedisPoolConfig(), redisProperties.getHost(), redisProperties.getPort());
    }
    

    And instead of using this;

    Jedis jedis = new Jedis();
    

    Use this one;

    Jedis jedis = pool.getResource();
    

    And the last step is;

    $ docker run -p 8080:8080 --net test-netw shortenurl