简体   繁体   English

Spring 在 docker 上启动无法从资源中读取属性文件,但是可以在没有 Docker 的情况下使用 Z93F725A4jar423D21C28

[英]Spring boot on docker not able to read a property file from resources, but the can without Docker with java -jar

My Spring boot application has a property file with AWS credentials, when I run gradle bootJar, the fat jar with all dependencies is built and I can run it with java -jar My Spring boot application has a property file with AWS credentials, when I run gradle bootJar, the fat jar with all dependencies is built and I can run it with java -jar

But when I bundle I build a Docker image, what I get is the following error, basically complaining that it can't find AWS credentials in the property file under resources但是当我捆绑我构建一个 Docker 图像时,我得到的是以下错误,基本上是抱怨它在资源下的属性文件中找不到 AWS 凭证

2021-03-23 17:51:01.542 ERROR 1 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain: [EnvironmentVariableCredentialsProvider: Unable to load AWS credentials from environment variables (AWS_ACCESS_KEY_ID (or AWS_ACCESS_KEY) and AWS_SECRET_KEY (or AWS_SECRET_ACCESS_KEY)), SystemPropertiesCredentialsProvider: Unable to load AWS credentials from Java system properties (aws.accessKeyId and aws.secretKey), WebIdentityTokenCredentialsProvider: To use assume role profiles the aws-java-sdk-sts module must be on the class path., com.amazonaws.auth.profile.ProfileCredentialsProvider@61a02480: profile file cannot be null, com.amazonaws.auth.EC2ContainerCredentialsProviderWrapper@23b9c9f8: Failed to connect to service endpoint: ]] with root cause

Note again that the I can run the assembled fat jar with java -jar that has the same credentials property file with no issues.再次注意,我可以运行组装的胖 jar 和 java -jar 具有相同的凭据属性文件没有问题。

This is my Dockerfile这是我的 Dockerfile

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG DEPENDENCY=target/dependency
COPY ${DEPENDENCY}/BOOT-INF/lib /app/lib
COPY ${DEPENDENCY}/META-INF /app/META-INF
COPY ${DEPENDENCY}/BOOT-INF/classes /app
ENTRYPOINT ["java","-cp","app:app/lib/*","com.myapp.myapp"]

And these are the docker tasks in build.gradle (after of course importing palantir) to build the image这些是 build.gradle 中的 docker 任务(当然是在导入 palantir 之后)来构建图像

apply plugin: 'com.palantir.docker'

group = 'com.myapp'

bootJar {
    baseName = 'mayapp'
    version =  '0.1.0'

}
task unpack(type: Copy) {
    dependsOn bootJar
    from(zipTree(tasks.bootJar.outputs.files.singleFile))
    into("build/dependency")
}
docker {
    name "${project.group}/${bootJar.baseName}"
    copySpec.from(tasks.unpack.outputs).into("dependency")
    buildArgs(['DEPENDENCY': "dependency"])
}

and this is how I run the container这就是我运行容器的方式

docker run -p 8080:8080 com.myapp/myapp

Should be ENTRYPOINT ["java","-cp","/app:/app/lib/*","com.myapp.myapp"] Not sure how your classpath was found with app:app/lib/*应该是ENTRYPOINT ["java","-cp","/app:/app/lib/*","com.myapp.myapp"]不确定如何使用app:app/lib/*找到您的类路径

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

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