繁体   English   中英

运行 Docker 内的 Spring 启动应用程序时出现异常

[英]Exception when running Spring Boot app inside of Docker

我在连接到配置服务器时遇到问题。 我不确定我做错了什么。 我已配置在端口 8888 上名为“config-server”的 docker 容器中运行的服务器。

http://config-server:8888. Will be trying the next url if available
2020-08-10 17:38:35.196 ERROR 11052 --- [           main] o.s.boot.SpringApplication               : Application run failed

java.lang.IllegalStateException: Could not locate PropertySource and the fail fast property is set, failing
    at org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:148) ~[spring-cloud-config-client-2.2.3.RELEASE.jar:2.2.3.RELEASE]

发现服务器 bootstrap.yml

spring:
  application:
    name: discovery-server
  cloud:
    config:
      uri: http://config-server:8888
      fail-fast: true
      retry:
        max-attempts: 20

编辑
配置服务器 Dockerfile

FROM openjdk:11.0-jre
ADD ./target/config-server-0.0.1-SNAPSHOT.jar config-server-0.0.1-SNAPSHOT.jar
ENTRYPOINT ["java", "-jar", "/config-server-0.0.1-SNAPSHOT.jar"]
EXPOSE 8888

docker 运行-p 8888:8888 --name config-server 3deb982c96fe
发现服务器未在 docker 中运行。 首先我要创建它的.jar文件

原始问题已经在评论中回答,在这里回答最后一点以获得更好的格式:

每次运行mvn clean installgradle build时,Jar 文件都将构建在应用程序的/target文件夹中。 In order to run this in Docker you have to copy the jar file from your /target directory to the Docker container inner files, and then run it ( java -jar nameOfYourJar.jar ).

您的 jar 的名称可以在 maven/gradle 设置中定义,但为了保持您的 Dockerfile 通用,我建议遵循 Dockerfile:

FROM openjdk:11.0-jre
ARG JAR_FILE=/target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","app.jar"]

with ARG JAR_FILE you save the path to any jar file (found in target) as JAR_FILE variable in docker and then you can copy it to your Docker inner files where it will be stored under the name app.jar .

ENTRYPOINT 是将在容器启动时运行的命令。

将 Dockerfile 放在/target目录旁边(因此在应用程序的根文件夹中)并在终端中运行以下命令:

docker build -t springapp. && docker run --rm -d -p 8080:8080 springapp

希望这可以澄清一切。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM