【问题标题】: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
【问题讨论】:
标签:
docker
dockerfile
devops
12factor
【解决方案1】:
入口点是秘密。
您有两种解决方案:
两种解决方案都有相同的 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