簡體   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