问题
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
回答1:
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/
回答2:
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.
来源:https://stackoverflow.com/questions/44125915/docker-compose-externalize-spring-application-properties