【问题标题】:How to change application properties of war file in Dockerfile如何在 Dockerfile 中更改 war 文件的应用程序属性
【发布时间】:2019-09-19 15:57:51
【问题描述】:

我有一个 Springboot 应用程序的 jar/war 文件,它包括 application.properties 文件(在 Dev 环境中有与数据库的连接设置,放在文件夹 src\main\resources,我使用 hikari 数据源)。现在,我想在创建 Dockerfile 时修改这个属性文件,为 UAT 环境构建一个镜像。我该怎么做?

这是我的 Dockerfile:

FROM openjdk:8
VOLUME /tmp
ADD target/springboot-docker-mysql.jar springboot-docker-mysql.jar
EXPOSE 8083
ENTRYPOINT ["java","-jar","springboot-docker-mysql.jar"]

这是我的属性文件:

## Spring DATA SOURCE Configurations
#spring.datasource.url = jdbc:mysql://localhost:3306/test?useSSL=false
#spring.datasource.url = jdbc:mysql://mysql-standalone:3306/test?autoReconnect=true&failOverReadOnly=false&maxReconnects=10&useSSL=false
spring.datasource.url = jdbc:mysql://mysql-standalone-p-lggjt-mysql.mysql-standalone-p-lggjt.svc.cluster.local:3306/test?useSSL=false
spring.datasource.username = testuser
spring.datasource.password = testuser@123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver


## Hibernate Properties
# The SQL dialect makes Hibernate generate better SQL for the chosen database
#spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = create

spring.jpa.generate-ddl=true
spring.jpa.show-sql=true

server.port=8083

【问题讨论】:

    标签: spring-boot docker dockerfile hikaricp


    【解决方案1】:

    更改您的Dockerfile

    FROM openjdk:8
    VOLUME /tmp
    WORKDIR /
    ADD target/springboot-docker-mysql.jar springboot-docker-mysql.jar
    ENTRYPOINT ["java","-jar","springboot-docker-mysql.jar","--spring.config.additional-location=application.properties"]
    

    更改docker run 命令:

    docker run --name my-app --restart always -it -d -p 8083:8083 -v $PWD/application.properties:/application.properties my-app:v1
    

    注意: $PWD/application.properties 是您的应用程序属性文件,您将其安装在容器内的 /application.properties 位置。我们还对 dockerfile 做了些微改动,并使用--spring.config.additional-location 选项指向 application.properties。

    要了解更多关于--spring.config.additional-location的信息,请查看this

    【讨论】:

    • 谢谢,我可以只需要更改 Dockerfile 和命令“Docker build”吗?因为我想用新配置创建一个镜像,所以我使用 Rancher (RKE) 来部署应用程序(我不使用 docker run 命令)
    • 在这种情况下,您可以在 docker 映像中静态复制 application.peroperties。 COPY application.properties /application.properties 然后构建 docker 镜像,它将在镜像中包含属性文件。
    • 如果您进一步想知道如何在 RKE 上部署它,请发布另一个问题。我可以在那里回答。
    猜你喜欢
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 2018-03-10
    • 1970-01-01
    • 2012-12-06
    相关资源
    最近更新 更多