Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker-compose - externalize spring application.properties

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

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?

Thank you for your help

like image 770
Jeffrey Avatar asked May 23 '17 04:05

Jeffrey


Video Answer


1 Answers

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

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

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

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/

like image 107
zeagord Avatar answered Sep 24 '22 17:09

zeagord