簡體   English   中英

使用 buildpacks 將 Spring Boot 應用程序構建為 docker 映像時,如何設置動態環境變量?

[英]How can I set a dynamic environment variable when building my Spring Boot app as a docker image using buildpacks?

如果您檢查示例 aws-apprunner-terraform 代碼(使用 petclinic)的提交,您會發現它們在 dockerfile 中包含以下內容:

ENTRYPOINT env spring.datasource.password=$(aws ssm get-parameter --name /database/password --with-decrypt --region $AWS_REGION | grep Value | cut -d '"' -f4) java -Djava.security.egd=file:/dev/./urandom -jar /app.jar

本質上,它是在運行時動態設置spring.datasource.password環境變量以從 AWS SSM 檢索值。 當使用 Dockerfile 時,這一切都很好。

但是當我使用 Spring Boot 的內置bootBuildImage任務(我使用 gradle)構建我的應用程序時,我不確定如何達到相同的效果。

使用 Spring Boot 提供的構建包時,如何將環境變量值設置為動態的,就像上面示例中所做的那樣?

您可以在 repo 的根目錄中創建一個.profile ,其內容如下:

export MY_VAR=$(some-dynamic-value)

更多信息: https://github.com/buildpacks/spec/blob/main/buildpack.md#app-interface

When using the Spring Boot Gradle plugin's bootBuildImage or the Maven plugin's spring-boot:build-image with the default Paketo buildpacks , you can use service bindings to provide external credentials.

要在本地進行測試,您可以執行以下操作:

$ mkdir -p bindings/db
$ echo "mysql" > bindings/db/type
$ aws ssm get-parameter --name /database/password --with-decrypt --region $AWS_REGION | grep Value | cut -d '"' -f4 > bindings/db/password

最終得到這個目錄結構:

bindings
└── db
    ├── password
    └── type

在容器中運行應用程序時,將bindings目錄掛載到容器並提供一個名為SERVICE_BINDING_ROOT的環境變量,該變量指向bindings目錄。 Paketo buildpacks 為應用程序映像貢獻的Spring Cloud Bindings庫將執行 rest。

我對 Terraform 知之甚少,無法建議如何最好地在 tf 腳本中實現這一點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM