简体   繁体   English

使用 gradle 任务构建映像时,无法在 docker 容器内设置环境变量

[英]Not able to set environment variables inside a docker container when building image using gradle task

I have written a gradle task buildDocker in my build.gradle file as follows:我在我的 build.gradle 文件中编写了一个 gradle 任务 buildDocker,如下所示:

task buildDocker(type: Docker, dependsOn: copyJar) {
    push = false
    baseImage "openjdk:8u151"
    println System.getenv('AWS_ACCESS_KEY_ID')
    println System.getenv('AWS_SECRET_ACCESS_KEY')
    setEnvironment('AWS_ACCESS_KEY_ID',System.getenv('AWS_ACCESS_KEY_ID'))
    setEnvironment('AWS_SECRET_ACCESS_KEY',System.getenv('AWS_SECRET_ACCESS_KEY'))
    runCommand("mkdir myProject")
    addFile("myProject-0.0.jar","myProject/")
    workingDir("/myProject")
    runCommand("unzip -q myProject-0.0.jar")
    exposePort(8888)
    entryPoint(["java","-XX:+UnlockExperimentalVMOptions","- 
XX:+UseCGroupMemoryLimitForHeap","-XX:MaxRAMFraction=1","-XshowSettings:vm","-cp", 
"/myProject-config:.","org.springframework.boot.loader.JarLauncher"])
}

When I build the image by running the task I am able to see the println outputs in the terminal.当我通过运行任务构建图像时,我能够在终端中看到 println 输出。 But when I run the image, I keep getting the following exception:但是当我运行图像时,我不断收到以下异常:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'simpleMessageListenerContainer' defined in class path resource [org/springframework/cloud/aws/messaging/config/annotation/SqsConfiguration.class]: Invocation of init method failed; nested exception is com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain: [com.amazonaws.auth.profile.ProfileCredentialsProvider@46244e65: profile file cannot be null]
Caused by: com.amazonaws.SdkClientException: Unable to load AWS credentials from any provider in the chain: [com.amazonaws.auth.profile.ProfileCredentialsProvider@46244e65: profile file cannot be null]

I have also tried setting the environment variables directly in the run statement by using the -e parameters in the run command and by mounting my ~/.aws/credentials file to the docker container, but with those also, I get the same exception.我还尝试通过使用 run 命令中的 -e 参数并将我的 ~/.aws/credentials 文件安装到 docker 容器中,直接在 run 语句中设置环境变量,但是对于这些,我也得到了相同的异常。

EDIT1: here's the docker run command using the -e flags: EDIT1:这是使用 -e 标志的 docker 运行命令:

docker run -e AWS_ACCESS_KEY_ID=XXXX -e AWS_SECRET_ACCESS_KEY=XXXX --link mysql --name myapp <imageName>

Also, put the credentials as key value pairs in a file-env.list and tried this:此外,将凭据作为键值对放入 file-env.list 并尝试以下操作:

docker run --env-file env.list -v --link mysql --name myapp <imagename>

Environment variables that are defined in scripts that run when building an image, are not kept when creating a container based on that image.在构建映像时运行的脚本中定义的环境变量在基于该映像创建容器时不会保留。

You need to explicitly set environment variables using ENV directive in your Dockerfile, or set them when running the container using the -e flag, for example.例如,您需要在 Dockerfile 中使用ENV指令显式设置环境变量,或者在运行容器时使用-e标志设置它们。

Please see this article for more information.请参阅这篇文章了解更多信息。

Finally got it working with mounting my credentials file to the docker container:终于可以将我的凭据文件安装到 docker 容器中:

docker run -p 8888:8888 -v ~/.aws:/root/.aws --link mysql --name myapp <imageName>

the credentials are created and stored in the.aws folder.凭据已创建并存储在 .aws 文件夹中。 The mistake I was doing earlier, with mounting was a stupid one, probably because of fatigue, I kept mounting it on the /home directory in the container but home folder (~) in the container is /root instead, so once I changed that, there weren't any issues.Though I still don't understand why the -e flags are not working, as according to this - https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/credentials.html the amazon java sdk will try to load the credentials from the env variables first and then from the java system properties and then from the default credential profiles file.我之前做的错误,挂载是一个愚蠢的错误,可能是因为疲劳,我一直将它挂载到容器中的 /home 目录,但容器中的主文件夹(~)是 /root,所以一旦我改变了它,没有任何问题。虽然我仍然不明白为什么 -e 标志不起作用,据此 - https://docs.aws.amazon.com/sdk-for-java/v1/developer -guide/credentials.html the amazon java sdk will try to load the credentials from the env variables first and then from the java system properties and then from the default credential profiles file.

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

相关问题 在 docker 镜像/容器中安装和使用 Gradle - Installing and using Gradle in a docker image/container 使用Palantir Gradle插件构建Docker容器时找不到.jar文件 - .jar file not found when building a Docker container with Palantir Gradle plug-in 摇篮停止建设任务 - Gradle stop building task 如何在 Tomcat docker 中设置不同的环境变量? - how to set differente environment variables in Tomcat docker? 在gradle.properties文件中使用环境变量 - Using environment variables in gradle.properties file 使用Java程序添加环境变量,并且能够查看何时从“我的电脑”中打开“环境”选项卡 - Adding environment variables using java program and able to see when I open environment tab from My Computer 在Docker-Java API中构建映像/容器时如何连接到自定义网络 - How to connect to custom network when building image/container in Docker-Java API 在 docker 容器内使用带有 ssl 的自托管(Jetty)元数据库时出错 - Error when using selfhosted (Jetty) Metabase with ssl inside docker container 无法使用运行时 exec 访问环境变量 - Not able to access environment variables using runtime exec 如何通过任务在gradle中设置环境变量? - How do set environment variable in gradle via task?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM