Springboot's docker startup parameter transfer

In the past few days, some netizens have asked how to pass spring.profiles.active=test when using docker, that is to say, springboot switches the configuration file. In the past, when we started the jar directly through java, we directly followed --spring.profiles.active=test or passed the jvm parameter -Dspring.profiles.active=test. Let me write some commands.

Springboot switch configuration file under non-docker

java -jar test.jar --spring.profiles.active=test #The first case switches profiles

java -Dspring.profiles.active=test -jar test.jar #The second case switches profiles

Some friends will wonder, how can I switch through jvm parameters, in fact, springboot supports it.

Springboot switch configuration file under docker

After docker encapsulates springboot, there will be more steps to switch, which is actually not difficult. The author has found many articles on the Internet, but I have not seen the environment variable passing parameters implemented by the docker packaging plug-in com.spotify:docker-maven-plugin. It may be too simple and no one has written it, but I see that many people need , let me write it. But there are Env variables passed through Dockerfile on the Internet. Below I post the packaged plugin code in the pom:

Note the change in env node and entryPoint

com.spotifydocker-maven-plugin0.4.11pumelo/${project.artifactId}-${version}frolvlad/alpine-oraclejdk8:slim""[ "sh", "-c", "java $JAVA_OPTS -jar /${project.build.finalName}.jar" ]/${project.build.directory}${project.build.finalName}.jar

test

After repackaging the image, run

docker run -e JAVA_OPTS='-Xms128m -Xmx256m -Dspring.profiles.active=prod' -it pumelo/test-1.0.0 /bin/bash

In this way, you can switch the configuration file of springboot through the jvm parameter.

Thank you to all the judges for coming.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324522757&siteId=291194637