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.


package org.assignments.dockerize;

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

import java.util.List;

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

    private final ProductsService productsService;
    private final CategoryService categoryService;

    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) {


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 {

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 {

tasks.withType( JavaCompile ).configureEach {
    options.forkOptions.jvmArgs.addAll( ['--add-opens', 'jdk.compiler/'] )


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

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 = true = org.hibernate.dialect.PostgreSQLDialect = 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>8899/tcp, :::2008->8899/tcp   jovial_rosalind
4c7d9b67a7ac   postgres:latest   "docker-entrypoint.s…"   2 weeks ago      Up 2 hours>5432/tcp, :::2006->5432/tcp   zs-intern-pgsql

C:UsersmasterProjects>docker port jovial_rosalind
8899/tcp ->
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