首页 > 解决方案 > 尝试运行 dockerized spring 应用程序时无法连接到 localhost

问题描述

我创建了一个弹簧启动应用程序。这在本地主机中工作正常。但是在对项目进行 dockerizing 时,我在连接到 localhost 时遇到了问题。即使在 docker CLI 的 CLI 中,它也会说:

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

但是我在终端中没有收到任何错误。每当我尝试通过终端访问localhost:2008/store/products时,终端都没有变化。

控制器

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);
    }
}

构建.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"]

应用程序属性

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

创建图像的代码

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

运行容器的代码

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

容器创建成功。端口也显示出来。

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

C:\Users\master\Projects>docker port jovial_rosalind
8899/tcp -> 0.0.0.0:2008
8899/tcp -> :::2008

但是当我尝试打开时localhost:2008/store/products,它说This page isn’t workinglocalhost didn’t send any data. ERR_EMPTY_RESPONSE

标签: javaspringpostgresqlspring-bootdocker

解决方案


如果没有来源,很难调查您面临的问题。我唯一可以建议的是尝试替换docker run -p 8085:8085 docker-spring-bootdocker run -p localhost:8085:localhost:8085 docker-spring-boot. 请让我知道它是否有效。


推荐阅读