How to implement Docker to dynamically pass parameters to Springboot projects

How to implement Docker to dynamically pass parameters to Springboot projects

background

Recently, some friends who are new to Docker asked me that they want to dynamically pass parameters to microservices through docker-compose.yml , instead of hard-coding them in the project configuration file every time, then building the service image, and finally packaging and publishing it through a series of processes to update the configuration. So, is it possible to directly put some configuration items into environment variables in docker-compose.yml , and then the springboot project automatically obtains parameters from the environment variables?

Scenario

Suppose there is a Springboot project with a database configuration item in it, but there are multiple database IPs in different database test environments (DEV\SIT\UAT). If you want to use the same Springboot project image, you can switch the database configuration at any time. In short, the database configuration of your Springboot application should be passed in externally instead of hardcoded.


There is a database configuration in the Springboot application as follows:

spring.datasource.url = jdbc:mysql://192.168.0.11:3306/db?useUnicode=true&characterEncoding=utf8
#Configure database username spring.datasource.username = sa
#Configure database password spring.datasource.password = sa

Solution

Use SpEL expressions to dynamically obtain database configuration from environment variables


Next, we replace the database configuration with a spEL expression in the Springboot configuration file

#Configure database link spring.datasource.url = jdbc:mysql://${DB_HOST}:${DB_PORT}/${DB_NAME}?useUnicode=true&characterEncoding=utf8
#Configure database username spring.datasource.username = ${DB_USER}
#Configure database password spring.datasource.password = ${DB_PASSWORD}

Configure our database parameters in docker-compose.yml

version: '3'
services:
 web:
  restart: always
  depends_on:
   -db
  image: springboot-app-image
  build: .
  ports:
   - 8080:8080
  environment:
   - DB_HOST=192.168.0.11
   - DB_PORT=3306
   - DB_USER=root
   - DB_PASSWORD=123456
   - DB_NAME=db
  networks:
   - credit-facility-net
  deploy:
   mode: replicated
   replicas: 3
   restart_policy:
    condition: on-failure
    delay: 5s
    max_attempts: 3
   update_config:
    parallelism: 1
    delay: 10s

In this way, our Springboot application can dynamically obtain the database configuration when starting the container service

This is the end of this article about how Docker dynamically passes parameters to Springboot projects. For more information about Docker Springboot dynamic parameter passing, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Implementation of spring boot front-end and back-end parameter transmission

<<:  Detailed explanation of MySQL data rows and row overflow mechanism

>>:  Four ways to modify the default CSS style of element-ui components in Vue

Recommend

Basic installation process of mysql5.7.19 under winx64 (details)

1. Download https://dev.mysql.com/downloads/mysql...

Example of how to set up a Linux system to automatically run a script at startup

Preface Hello everyone, I am Liang Xu. At work, w...

PostgreSQL materialized view process analysis

This article mainly introduces the process analys...

How to distinguish MySQL's innodb_flush_log_at_trx_commit and sync_binlog

The two parameters innodb_flush_log_at_trx_commit...

A brief discussion on JS packaging objects

Table of contents Overview definition Instance Me...

How to implement Docker Registry to build a private image warehouse

The image of the microservice will be uploaded to...

Example code for using Nginx to implement 301 redirect to https root domain name

Based on SEO and security considerations, a 301 r...

HTML+CSS to achieve responsive card hover effect

Table of contents accomplish: Summarize: Not much...

Graphic tutorial on configuring log server in Linux

Preface This article mainly introduces the releva...

MySQL full-text fuzzy search MATCH AGAINST method example

MySQL 4.x and above provide full-text search supp...

Seven ways to implement array deduplication in JS

Table of contents 1. Using Set()+Array.from() 2. ...

Enable sshd operation in docker

First, install openssh-server in docker. After th...