我有一个spring boot应用程序,它连接到mongo db并使用docker部署应用程序。我正在使用这个docker-compose.yml文件,它工作正常:
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 app docker image(spring-app)中的application.properties文件。如何使用docker-compose外化/传入application.properties文件?
感谢您的帮助
答案 0 :(得分:4)
您必须根据需要使用Spring Profiles定义环境变量。
server:
port: 9000
---
spring:
profiles: development
server:
port: 9001
---
spring:
profiles: production
server:
port: 0
您可以定义在运行时需要选择哪个配置文件。
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)随处可用,可帮助您将配置外部化。
http://cloud.spring.io/spring-cloud-static/spring-cloud-config/1.3.0.RELEASE/
答案 1 :(得分:0)
我不确定我是否会回答这个问题,实际上我是否理解这一点。但是,这将是我的2美分:
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
我的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
对我来说,工作完全正常且稳定。