admin管理员组

文章数量:1389903

1. my springboot application is like this:

-- src
  -- main
    -- java
      -- Application.java
    -- resource
      -- application.properties
  -- test
-- pom.xml

Application.java

@RestController
@SpringBootApplication
public class Application {

    @RequestMapping("/")
    public String home() {
        return "Hello Docker World";
    }

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}

application.properties

spring.application.name=demo

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=".0.0" xmlns:xsi=";
    xsi:schemaLocation=".0.0 .0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

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

    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <url/>
    <licenses>
        <license/>
    </licenses>
    <developers>
        <developer/>
    </developers>
    <scm>
        <connection/>
        <developerConnection/>
        <tag/>
        <url/>
    </scm>
    <properties>
        <java.version>21</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

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

</project>

I packaged it into a jar file, demo.jar

2. my Dockerfile is like this:

FROM registry.redhat.io/ubi9/openjdk-21-runtime
COPY demo.jar app.jar
ENTRYPOINT ["java","-jar","app.jar"]

registry.redhat.io/ubi9/openjdk-21-runtime is red-hat offical images, it base on the osbs-ubi9-minimal with a jre 21. get this image

i make a image by this Dockerfile that contains the registry.redhat.io/ubi9/openjdk-21-runtimeand image and the demo.jar. the image name is songjianhan/demo:latest

you can pull it by using docker command: docker pull songjianhan/demo:latest

3. my docker run command is like this:

docker run -d -p 8080:8080 --name demo songjianhan/demo:latest

docker logs demo

the logs is:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.4.3)

2025-03-15T06:02:01.882Z  INFO 1 --- [demo] [           main] com.example.demo.Application             : Starting Application v0.0.1-SNAPSHOT using Java 21.0.6 with PID 1 (/home/default/app.jar started by default in /home/default)
2025-03-15T06:02:01.885Z  INFO 1 --- [demo] [           main] com.example.demo.Application             : No active profile set, falling back to 1 default profile: "default"
2025-03-15T06:02:02.669Z  INFO 1 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2025-03-15T06:02:02.687Z  INFO 1 --- [demo] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2025-03-15T06:02:02.687Z  INFO 1 --- [demo] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.36]
2025-03-15T06:02:02.707Z  INFO 1 --- [demo] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2025-03-15T06:02:02.708Z  INFO 1 --- [demo] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 763 ms
2025-03-15T06:02:02.994Z  INFO 1 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path '/'
2025-03-15T06:02:03.007Z  INFO 1 --- [demo] [           main] com.example.demo.Application             : Started Application in 1.561 seconds (process running for 2.02)

4. my question

  1. if i run the command:

docker exec demo curl localhost:8080/

output is:

 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    18  100    18    0     0    219      0 --:--:-- --:--:-- --:--:--   219
Hello Docker World

it works very well

  1. if i run the command like

curl localhost:8080/

output is:

curl: (56) Recv failure: The connection was reset by the peer.
  1. if i run the command: docker ps

output is:

0540f11acf75   songjianhan/demo:latest   "java -jar app.jar"   6 minutes ago   Up 6 minutes   0.0.0.0:8080->8080/tcp, [::]:8080->8080/tcp, 8443/tcp   demo

How can i access the interface from outside?

I could imagine this happening beacause of the UBI firewall but when I use eclipse-temurin:21 as my base image, the same thing happens.

本文标签: springWhy can39t I access the springboot program interface in my docker containerStack Overflow