Spring boot + docker运行时定制环境变量

1、目的

在docker run时,导入一个运行时参数到Spring boot application中。

2、方式

采用环境变量,以变量HELLO_WORLD为类

3、步骤

step 1. Dockerfile中添加

ENV HELLO_WORLD="this is default hello world"

step 2. Spring Boot中获取环境变量:

@Configuration
@Slf4j
public class HelloWorldEnv implements EnvironmentAware {
    private String helloWorld;

    public String getHelloWorld() {
        return this.helloWorld;
    }

    @Override
    public void setEnvironment(Environment environment) {
        this.helloWorld = environment.getProperty("HELLO_WORLD");
        log.info("ENV HELLO_WORLD={}", getEndPoint());
    }
}

step 3. docker build 打出镜像

step 4. docker run -e HELLO_WORLD="my hello world" your-docker-image:version

docker run时如果不带-e HELLO_WORLD="...",那么Spring boot中将使用默认的"this is default hello world"

完结:enjoy docker


版权声明:本文为junlon2006原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。