Can not connect to localhost while trying to run a dockerized spring application

  docker, java, postgresql, spring, spring-boot

I have created a spring boot application. This works fine in localhost. But upon dockerizing the project I am facing issues in connecting to the localhost. Even in the CLI of the docker CLI it says:

sh-4.4# curl localhost:2008/store/products
curl: (7) Failed to connect to localhost port 2008: Connection refused

Yet I am getting no errors in the terminal. Whenever I try to access through localhost:2008/store/products there is no change in the terminal.

Controller

package org.assignments.dockerize;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/store/")
class ProductsCategoriesController {

    private final ProductsService productsService;
    private final CategoryService categoryService;


    @Autowired
    public ProductsCategoriesController(ProductsService productsService, CategoryService categoryService) {
        this.productsService = productsService;
        this.categoryService = categoryService;
    }

    @GetMapping(path = "/products")
    public List<Products> getProducts() {
        return productsService.getProducts();
    }
    @PostMapping(path = "/products")
    public Products addNewProduct(@RequestBody Products product) {
        return productsService.addNewProduct(product);
    }

    @GetMapping(path = "/products/{id}")
    public List<String> findProductByCategoryID(@PathVariable("id") Integer id) {
        return productsService.getProductsByCategoryID(id);
    }

    @GetMapping(path = "/categories")
    public List<Categories> getCategories() {
        return categoryService.getCategories();
    }
    @PostMapping(path = "/add/categories")
    public void addNewCategory(@RequestBody Categories category) {
        categoryService.addNewCategory(category);
    }
}

build.gradle

plugins {
    id 'org.springframework.boot' version '2.5.2'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'org.assignments'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '15'
def versionLog4j = '2.14.1'
def versionLombok = '1.18.20'
repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: versionLog4j
    implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: versionLog4j
    implementation group: 'org.projectlombok', name: 'lombok', version: versionLombok
    annotationProcessor group: 'org.projectlombok', name: 'lombok', version: versionLombok
//    implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    runtimeOnly 'org.postgresql:postgresql'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
    useJUnitPlatform()
}

tasks.withType( JavaCompile ).configureEach {
    options.forkOptions.jvmArgs.addAll( ['--add-opens', 'jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED'] )
}

Dockerfile

FROM openjdk:16
EXPOSE 8899
ADD Assignment-14a/build/libs/*.jar dockerstore.jar
ENTRYPOINT ["java","-jar","dockerstore.jar"]

application.properties

server.port = 8090
spring.datasource.url = jdbc:postgresql://<myIP>:2006/store
spring.datasource.username = postgres
spring.datasource.password = *****
spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.show-sql = true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.format_sql = true

Code to create image

docker build -f Assignemnt-14a/Dockerfile -t storeimage .

Code to run the container

docker run -p 8085:8085 docker-spring-boot

The container gets created successfully. The port is also shown.

C:UsersmasterProjects>docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                       NAMES
3a3ad3d11b63   storeimage        "java -jar dockersto…"   31 minutes ago   Up 31 minutes   0.0.0.0:2008->8899/tcp, :::2008->8899/tcp   jovial_rosalind
4c7d9b67a7ac   postgres:latest   "docker-entrypoint.s…"   2 weeks ago      Up 2 hours      0.0.0.0:2006->5432/tcp, :::2006->5432/tcp   zs-intern-pgsql

C:UsersmasterProjects>docker port jovial_rosalind
8899/tcp -> 0.0.0.0:2008
8899/tcp -> :::2008

But when I try to open localhost:2008/store/products, it says This page isn’t workinglocalhost didn’t send any data. ERR_EMPTY_RESPONSE

Source: Docker Questions

LEAVE A COMMENT