简体   繁体   English

如何在 docker 中动态配置应用程序属性文件

[英]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)

solution A - entrypoint:解决方案 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 $@

To create a container from this solution:要从此解决方案创建容器:

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

solution B - entrypoint:解决方案 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 $@

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.

相关问题 Docker - 如何动态配置使用平面文本配置文件的应用程序? - Docker - how to dynamically configure an application that uses flat text config files? 如何使用外部应用程序属性文件运行 docker 容器? - how to run docker container with external application properties file? 如何配置动态配置的 Docker 代理 - How to configure dynamically provisioned Docker agents 如何在 docker 中配置 nginx 和乘客到轨道应用 - How to configure nginx and passenger to rails application in docker 如何配置 Hibernate ORM 数据库属性以与 Docker 一起使用 - How to configure Hibernate ORM database properties to work with Docker 如何在 Docker 文件中配置 Databricks 令牌 - How to configure Databricks token inside Docker File 如何从 docker-compose 文件中的环境变量中选择应用程序属性 - How to make application properties pick from environment variables in docker-compose file 如何将spring.properties文件提供给spring boot docker镜像作为输入 - How to provide application.properties file to spring boot docker image as input 在 docker compose 文件中访问 Spring Boot 应用程序属性 - Access Spring boot application properties in docker compose file 为什么是 ! 在docker上的spring-boot应用程序的属性文件路径中? - Why is ! in the properties file path in spring-boot application on the docker?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM