在Docker容器中运行Spring Boot应用程序时发生异常。

huangapple go评论75阅读模式
英文:

Exception when running Spring Boot app inside of Docker

问题

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

http://config-server:8888
如果可用将尝试下一个 URL
2020-08-10 17:38:35.196 ERROR 11052 --- [ main] o.s.boot.SpringApplication : 应用程序运行失败

java.lang.IllegalStateException: 无法找到属性源并且设置了快速失败属性因此失败
在 org.springframework.cloud.config.client.ConfigServicePropertySourceLocator.locate(ConfigServicePropertySourceLocator.java:148) ~[spring-cloud-config-client-2.2.3.RELEASE.jar:2.2.3.RELEASE]

discovery-server bootstrap.yml

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

编辑

config-server 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 容器:

docker run -p 8888:8888 --name config-server 3deb982c96fe

发现服务器未在 Docker 中运行。首先,我想创建它的 .jar 文件。

英文:

I have a problem connecting to the config-server. I am not sure what am I doing wrong. I have configured server running in a docker container named "config-server" on port 8888.

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]

discovery-server bootstrap.yml

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

EDIT
config-server 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 run -p 8888:8888 --name config-server 3deb982c96fe
Discavery-server is not running in docker. First I want to create its .jar file

答案1

得分: 0

以下是翻译好的部分:

Jar文件将在每次运行mvn clean installgradle build时构建在应用程序的/target文件夹中。为了在Docker中运行它,您必须将jar文件从您的/target目录复制到Docker容器内部文件中,然后运行它(java -jar nameOfYourJar.jar)。

您可以在Maven/Gradle设置中定义jar文件的名称,但为了使您的Dockerfile通用,建议使用以下Dockerfile:

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

使用ARG JAR_FILE将jar文件的路径保存为JAR_FILE变量,然后将其复制到Docker内部文件中,其中它将以名称app.jar存储。

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

将Dockerfile放在/target目录旁边(即在您的应用程序的根目录中),然后在终端中运行以下命令:

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

希望这解释清楚了一切。

英文:

Original question already answered in comments, answering the last point here for better formatting:

Jar file will be built in /target folder of your application everytime you run mvn clean install or gradle build. 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).

Name of your jar can be defined in maven/gradle settings but to keep your Dockerfile generic I suggest following 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 is the command that will be run on container start.

Place the Dockerfile next to the /target directory (so in root folder of your app) and run following command in terminal:

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

Hope this clarifies everything.

huangapple
  • 本文由 发表于 2020年8月10日 23:40:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63343417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定