docker-compose-外部化spring application.properties

问题描述:

我有一个Spring Boot应用程序,该应用程序连接到mongo db,并使用docker部署了该应用程序。我正在使用此docker-compose.yml文件,该文件工作正常:

I have a spring boot application that connects to a mongo db and deployed the app with docker. I am using this docker-compose.yml file, which works fine:

version: '2'
services:
  db:
      container_name: app-db
      image: mongo
      volumes:
        - /data/db:/data/db
      ports:
        - 27017:27017
  web:
    container_name: spring-app
    image: spring-app
    depends_on:
      - db
    environment:
      SPRING_DATA_MONGODB_URI: mongodb://db:27017/appDB
      SPRING_DATA_MONGODB_HOST: db
    ports:
      - 8080:8080

当前,该应用程序使用嵌入在spring应用程序docker映像(spring-app)中的application.properties文件。如何使用docker-compose来外部化/传递application.properties文件?

Currently, the app is using the application.properties file embedded in the spring app docker image (spring-app). How do I externalize / pass-in the application.properties file using docker-compose?

感谢您的帮助

您必须根据需要使用Spring Profiles定义环境变量。

You must make use of the Spring Profiles to define the environment variables depending on your requirement.

server:
    port: 9000
---

spring:
    profiles: development
server:
    port: 9001

---

spring:
    profiles: production
server:
    port: 0

参考: https://docs.spring.io/spring-boot/docs/current/reference/ html / howto-properties-and-configuration.html#howto-change-configuration-depending-on-environment

您可以定义所需的配置文件在跑步期间被捡起e。

You can define which profile needs to be picked up during the runtime.

version: '2'
services:
  db:
      container_name: app-db
      image: mongo
      volumes:
        - /data/db:/data/db
      ports:
        - 27017:27017
  web:
    container_name: spring-app
    image: spring-app
    depends_on:
      - db
    environment:
      SPRING_DATA_MONGODB_URI: mongodb://db:27017/appDB
      SPRING_DATA_MONGODB_HOST: db
      SPRING_PROFILES_ACTIVE=development
    ports:
      - 8080:8080

但是,如果配置发生变化,这将要求您重建docker镜像,这是不理想的。 Spring Cloud Config(Vault)随手可得,可帮助您外部化配置。

But this will require you to rebuild the docker image if there is a change in the configuration which is not ideal. Here comes the Spring Cloud Config (Vault) comes in handy which helps you to externalize your configuration.

http://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.3.0 .RELEASE /