当前位置: 首页 > 知识库问答 >
问题:

如何将Spring-Boot Web服务转换为Docker映像?

高峻
2023-03-14

我想从Docker容器访问我的网站,但我不能。我尝试实现的步骤如下。完成所有步骤后,我无法访问http://localhost:8080/indexpage,我在哪里犯了错误?

Spring Boot项目名为demo。我的部分代码:

package com.qwerty.demo.rest;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class FunRestService {

    @GetMapping("/index")
    public String setIndex() {
        return "HELLO WORLD!";
    }
}

我的dockerfile代码:

FROM openjdk:8
COPY . /usr/var/www/MYPROJECT
WORKDIR /usr/var/www/MYPROJECT
EXPOSE 8080
CMD ./mvnw spring-boot:run

我使用此命令将Spring Boot项目构建为myimage1

docker build -t myimage1 .

然后,使用此命令从myimage1创建一个新容器。

docker run --name mycontainer1 myimage1

Maven为我下载必要的文件并启动我的应用程序。最后输出:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-01-19 17:40:32.604  INFO 6 --- [           main] com.qwerty.demo.DemoApplication     : Starting DemoApplication on 8086b6e010fb with PID 6 (/usr/var/www/MYPROJECT/target/classes started by root in /usr/var/www/MYPROJECT)
2019-01-19 17:40:32.613  INFO 6 --- [           main] com.qwerty.demo.DemoApplication     : No active profile set, falling back to default profiles: default
2019-01-19 17:40:34.119  INFO 6 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2019-01-19 17:40:34.170  INFO 6 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-01-19 17:40:34.171  INFO 6 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.14]
2019-01-19 17:40:34.186  INFO 6 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/usr/java/packages/lib/amd64:/usr/lib/x86_64-linux-gnu/jni:/lib/x86_64-linux-gnu:/usr/lib/x86_64-linux-gnu:/usr/lib/jni:/lib:/usr/lib]
2019-01-19 17:40:34.288  INFO 6 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-01-19 17:40:34.289  INFO 6 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1559 ms
2019-01-19 17:40:34.602  INFO 6 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-01-19 17:40:34.882  INFO 6 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''2019-01-19 17:40:34.888  INFO 6 --- [           main] com.qwerty.demo.DemoApplication     : Started DemoApplication in 3.176 seconds (JVM running for 69.839)

我们应该怎么做才能将一个这样的Spring-Boot项目(使用Dockerfile)转换成图像?如何访问我的网页?

共有2个答案

贺飞
2023-03-14

要完全解决您的问题(例如,在开发阶段,将Spring Boot项目归档并在本地浏览器中浏览相应的webapp),必须完成三个独立的任务:

>

  • 使用从Docker缓存机制中受益的Dockerfile来利用Docker映像的构建(以避免每次从头开始重新下载Maven依赖项,从而加快构建速度)

    确保Spring Boot应用程序监听指定端口的0.0.0.0特殊IP,而不是localhost

    最后发布给定的端口,以便您可以运行,例如:

     $ xdg-open http://localhost:8080/index
    

    第3步在@Poger的回答中得到了很好的解释,因此我将仅对第1步和第2步进行详细说明:

    >

  • 我在这篇文章中提出了一个Dockerfile:如何在Docker中缓存maven依赖项,灵感来自这篇博客文章,它一般适用于Java/maven项目(不仅仅是Spring Boot项目):

     # our base build image
     FROM maven:3-jdk-8 as maven
    
     WORKDIR /app
    
     # copy the Project Object Model file
     COPY pom.xml pom.xml
    
     # fetch all dependencies
     RUN mvn dependency:go-offline -B
    
     # copy your other files
     COPY src src/
    
     # build for release
     # NOTE: my-project-* should be replaced with the proper prefix
     RUN mvn package && cp target/my-project-*.jar app.jar
    
    
     # smaller, final base image
     FROM openjdk:8-jre-alpine
     # OPTIONAL: copy dependencies so the thin jar won't need to re-download them
     # COPY --from=maven /root/.m2 /root/.m2
    
     # set deployment directory
     WORKDIR /app
    
     # copy over the built artifact from the maven image
     COPY --from=maven /app/app.jar app.jar
    
     # set the startup command to run your binary
     CMD ["java", "-jar", "/app/app.jar"]
    

    但要进一步细化它,请注意,建议传递一个额外的系统属性java。安全egd

     CMD ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/app.jar"]
    

    或者如果您更喜欢ENTRYPOINT而不是CMD

     ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app/app.jar"]
    

    正如SO线程中提到的,如何访问在docker容器中运行的spring应用程序?,在容器化应用程序的上下文中,应避免使用localhost,并将其替换为0.0。0.0大多数情况下(即,如果应用程序应作为web服务响应来自容器外部的传入请求)。

    总之,您应该尝试在您的application.properties文件中添加以下行:

     server.address=0.0.0.0
    

  • 夏谦
    2023-03-14

    在运行Docker容器时,默认情况下不会发布任何应用程序在其中侦听的所有端口。

    为了发布端口,您需要在使用映像运行容器时指定它。有关如何执行此操作的所有详细信息,您可以查看docker run命令文档中的“公开”部分:https://docs.docker.com/engine/reference/run/

    简而言之,您希望在运行容器时添加另一个选项:

    docker run --name mycontainer1 -p 8080:8080 myimage1
    

    我不确定您是否希望通过添加

    EXPOSE 8080
    

    在您的Dockerfile中。实际上,这并不意味着在使用映像运行容器时端口将被暴露。正如您可能在Dockerfile reference中找到的:

    EXPOSE指令实际上并不发布端口。它作为构建映像的人和运行容器的人之间的一种留档,打算发布哪些端口。要在运行容器时实际发布端口,请在docker run上使用-p标志发布和映射一个或多个端口,或使用-P标志发布所有公开的端口并将其映射到高阶端口。

     类似资料:
    • 问题内容: 我看到可以将Docker映像转换为Vagrant框。 有没有一种方法可以将Vagrant框转换为Docker映像? 问题答案: 我在Github项目blacklabelops / centos中 使用EC2框和Virtualbox框重播了此内容。我已经准备了具有必要安装的Vagrantfile,您可以在Vagrant盒中尝试一下。 EC2盒: 使用https://github.com/

    • 我是java新手,正在尝试学习objectmapper。我正在使用它将地图转换为pojo。地图中的键是字符串,所有值都是字符串值,除了我想转换为地图的值。请仔细阅读下面的示例代码,以获得更清晰的图片。 POJO类: 测试代码: 例外: 尝试的变体选项: 我知道我可以将map1字段也保留为String,然后使用另一个对象映射器实例将其转换为map,但我想避免它。有没有办法直接将测试代码中的字符串转换

    • 很抱歉重复了这个问题,但我的问题是其他的。我有一个从json-string解析到map的JSON解析器方法。但是json-string有一个值,这个值也是json-string。大概是这样的: 因此,我的解析方法: 我在客户端得到响应: ?但是随后我得到一个错误,IDE告诉我,是一个字符串。 那么,我如何用我的UserData获得这个LinkedHashMap呢?对不起,为了我的英语。谢谢你。

    • 我有以下代码,希望使用Java8将列表转换为。 当我尝试将列表中的单个值映射为映射的键时,我得到了一个错误。

    • 我一直在研究Jackson,但似乎必须将映射转换为JSON,然后将生成的JSON转换为POJO。 有没有一种方法可以将地图直接转换成POJO?

    • 我已经用字符串类型和格式:date-time字段大摇大摆地构建了一个openapi模式。在示例窗口中,它向我展示了 “reportingDateTime”:“2022-02-02T10:56:33.310Z” . 但当我呼叫我的服务时,它会响应 "reportingDateTime":1639746778.200000000 我使用openapi生成器maven插件版本5.1.0生成spring