docker-compose - externalize spring application.properties

Viewed 11209

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

2 Answers

I am not sure if I will answer the question, actually if I understood that. But here will go, my 2 cents:

Docker-compose:

version: "3.4"

x-common-variables:
  &db-env-vars
    POSTGRES_DB: anime
    POSTGRES_USER: root
    POSTGRES_PASSWORD: root
    PORT_API: 8383

volumes:
  webflux_data:

networks:
  webflux_rede:

services:
  db-compose:
    container_name: db-postgres
    image: postgres:9.5-alpine
    hostname: postgres # postgres hostname
    restart: always
    ports:
      - 5432:5432
    volumes:
      - webflux_data:/var/lib/postgresql/data
    networks:
      - webflux_rede
    environment:
      *db-env-vars

web-api:
    image: pauloportfolio/web-api
    build:
      context: ./
      dockerfile: ./dev-dockerfile
      args:
        JAR_FILE: target/spring-webflux-essentials-0.0.1-SNAPSHOT.jar
    ports:
      - 8383:8383
      - 5005:5005
    volumes:
      - webflux_data:/var/lib/postgresql/data
    depends_on:
      - db-compose
    networks:
      - webflux_rede
    environment:
      DB_URL: r2dbc:pool:postgresql://db-compose:${PORT_DB}/${POSTGRES_DB}
      <<: *db-env-vars
      PROFILE: dev
      DEBUG_OPTIONS: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -Xmx1G -Xms128m -XX:MaxMetaspaceSize=128m
      

My application-dev.properties

server.port=${PORT_API}

# DB-VERSION-MIGRATION
r2dbc.migrate.resources-path=classpath:/db/migration/postgresql/*.sql
r2dbc.migrate.dialect=postgresql

# POSTGRES-R2DBC - RODA COM COMPOSE
spring.r2dbc.url=${DB_URL}
spring.r2dbc.name=${POSTGRES_DB}
spring.r2dbc.username=${POSTGRES_USER}
spring.r2dbc.password=${POSTGRES_PASSWORD}
spring.r2dbc.pool.validation-query=SELECT 1

For me, works completely fine and stable.

Related