[英]How to configure application properties file dynamically in docker
I have a jar file that contains the application.properties file.我有一个包含 application.properties 文件的 jar 文件。 can we configure the IP address and port number & username & password while running the docker image我们可以在运行 docker 映像时配置 IP 地址和端口号以及用户名和密码吗
Properties file location属性文件位置
App/bin/config/application.properties应用程序/bin/config/application.properties
Following are the application.properties以下是 application.properties
driverClassName = org.postgresql.Driver
url = jdbc:postgresql://localhost:5432/sakila
username = root
password = root
entrypoint is the secret.入口点是秘密。
You have two solutions:你有两个解决方案:
design the image to receive these parameters thru environment variables, and let the ENTRYPOINT injects them inside App/bin/config/application.properties
设计图像以通过环境变量接收这些参数,并让 ENTRYPOINT 将它们注入App/bin/config/application.properties
design the image to listen to a directory.设计图像以收听目录。 If this directory contains *.properties
files, the ENTRYPOINT will collect these files and combine them into one file and append the content with App/bin/config/application.properties
如果此目录包含*.properties
文件,ENTRYPOINT 将收集这些文件并将它们合并为一个文件和 append 内容与App/bin/config/application.properties
Both solutions have the same Dockerfile两种解决方案具有相同的 Dockerfile
From java:x
COPY jar ...
COPY myentrypoint /
ENTRYPOINT ["bash", "/myentrypoint"]
But not the same ENTRYPOINT (myentrypoint)但不一样的ENTRYPOINT(myentrypoint)
#!/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 $@
To create a container from this solution:要从此解决方案创建容器:
docker run -it -e DB_URL=jdbc:postgresql://localhost:5432/sakila myimage
#!/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 $@
To create a container from this solution:要从此解决方案创建容器:
docker run -it -v ./folder-has-props-files:/etc/java/props.d myimage
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.