docker-compose-外部化spring application.properties [英] docker-compose - externalize spring application.properties

查看:448
本文介绍了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 /

这篇关于docker-compose-外部化spring application.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆