【问题标题】:How to configure application properties file dynamically in docker如何在 docker 中动态配置应用程序属性文件
【发布时间】:2020-07-11 16:30:24
【问题描述】:

我有一个包含 application.properties 文件的 jar 文件。 我们可以在运行 docker 镜像时配置 IP 地址和端口号以及用户名和密码吗

属性文件位置

App/bin/config/application.properties

以下是application.properties

driverClassName = org.postgresql.Driver
url = jdbc:postgresql://localhost:5432/sakila
username = root
password = root

【问题讨论】:

  • 您好,欢迎来到 SO,您能否请 share the details 提供您用于构建和创建镜像的 docker 镜像和命令文件?

标签: docker dockerfile devops 12factor


【解决方案1】:

入口点是秘密。

您有两种解决方案:

  • 设计图像通过环境变量接收这些参数,并让ENTRYPOINT将它们注入到App/bin/config/application.properties

  • 设计图像以收听目录。如果该目录包含*.properties文件,ENTRYPOINT将收集这些文件并将它们合并为一个文件,并将内容附加App/bin/config/application.properties

两种解决方案都有相同的 Dockerfile

From java:x

COPY jar ...
COPY myentrypoint /
ENTRYPOINT ["bash", "/myentrypoint"]

但不是同一个ENTRYPOINT(myentrypoint)

解决方案 A - 入口点:

#!/bin/bash
# if the env var DB_URL is not empty
if [ ! -z "${DB_URL}" ]; then
  
   echo "url=${DB_URL}" >> App/bin/config/application.properties
fi
# do the same for other props
#...
exec call-the-main-entry-point-here $@

从这个解决方案创建一个容器:

 docker run -it -e DB_URL=jdbc:postgresql://localhost:5432/sakila myimage

解决方案 B - 入口点:

#!/bin/bash

# if /etc/java/props.d/ is a directory
if [ -d "/etc/java/props.d/" ]; then
   cat /etc/java/props.d/*.properties
   awk '{print $0}' /etc/java/props.d/*.properties >> App/bin/config/application.properties
fi

#...
exec call-the-main-entry-point-here $@

从这个解决方案创建一个容器:

 docker run -it -v ./folder-has-props-files:/etc/java/props.d myimage

【讨论】:

    猜你喜欢
    • 2019-04-02
    • 2019-09-09
    • 2017-11-09
    • 1970-01-01
    • 2016-08-24
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 2017-10-05
    相关资源
    最近更新 更多