Starting Spring Boot Application in Docker With Profile – 在Docker中用配置文件启动Spring Boot应用程序

最后修改: 2022年 5月 9日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

We all know how popular Docker is and how fashionable for Java developers it is to containerize their Spring Boot applications. However, how we set profiles in a dockerized Spring Boot application can be a question for some developers.

我们都知道Docker有多流行,对Java开发者来说,将Spring Boot应用容器化是多么的时尚。但是,对于一些开发者来说,我们如何在Docker化的Spring Boot应用程序中设置配置文件可能是一个问题。

In this tutorial, we will explain how to start a Spring Boot application with profiles when it’s in a Docker container.

在本教程中,我们将解释如何在Docker容器中启动带有配置文件的Spring Boot应用程序。

2. Basic Dockerfile

2.基本Dockerfile

Generally, to dockerize a Spring Boot application, we simply provide a Dockerfile.

一般来说,要对Spring Boot应用程序进行停靠,我们只需提供一个Dockerfile

Let’s have a look at a minimal Dockerfile for our Spring Boot application:

让我们看看Spring Boot应用程序的最小Dockerfile

FROM openjdk:11
COPY target/*.jar app.jar
ENTRYPOINT ["java", "-jar", "/app.jar"]

Surely, we can build our Docker image via docker build:

当然,我们可以通过docker build构建我们的Docker镜像。

docker build --tag=docker-with-spring-profile:latest .

Thus, we can run our application from the image docker-with-spring-profile:

因此,我们可以从镜像docker-with-spring-profile中运行我们的应用程序。

docker run docker-with-spring-profile:latest

As we notice, our Spring Boot application starts with the “default” profile:

正如我们注意到的,我们的Spring Boot应用程序以“default”配置文件开始。

2022-04-22 22:34:25.268 INFO 1 --- [main] c.b.docker.spring.DemoApplication: Starting DemoApplication using Java 11.0.14.1 on ea8851bea75f with PID 1 (/app.jar started by root in /)
2022-04-22 22:34:25.270 INFO 1 --- [main] c.b.docker.spring.DemoApplication: No active profile set, falling back to 1 default profile: "default"
//...

3. Setting Profile in Dockerfile

3.在Dockerfile中设置配置文件

One way to set a profile for our dockerized application is using Spring Boot’s command-line argument “-Dspring.profiles.active”.

为我们的docker化应用程序设置配置文件的一种方法是使用Spring Boot的命令行参数“-Dspring.profiles.active”

So, to set the profile as “test”, we add a new argument, “-Dspring.profiles.active=test”, to our Dockerfile’s ENTRYPOINT line:

因此,为了将配置文件设置为“test”,我们在Dockerfile的ENTRYPOINT行中添加一个新参数,“-Dspring.profiles.active=test”,

//...
ENTRYPOINT ["java", "-Dspring.profiles.active=test", "-jar", "/app.jar"]

To see the profile change, let’s run our container again with the same command:

为了看到配置文件的变化,让我们用同样的命令再次运行我们的容器。

docker run docker-with-spring-profile:latest

Accordingly, we can see the profile “test” is successfully picked up by our application:

因此,我们可以看到配置文件“test”被我们的应用程序成功接收。

2022-04-22 22:39:33.210 INFO 1 --- [main] c.b.docker.spring.DemoApplication: Starting DemoApplication using Java 11.0.14.1 on 227974fa84b2 with PID 1 (/app.jar started by root in /)
2022-04-22 22:39:33.212 INFO 1 --- [main] c.b.docker.spring.DemoApplication: The following 1 profile is active: "test"
//...

4. Setting Profile Using Environment Variables

4.使用环境变量设置配置文件

Sometimes, it’s not convenient to use a hard-coded profile in our Dockerfile. If we need more than one profile, picking one of them could be cumbersome when we run our container.

有时,在我们的Dockerfile中使用一个硬编码的配置文件并不方便。如果我们需要一个以上的配置文件,当我们运行我们的容器时,挑选其中的一个可能会很麻烦。

Nevertheless, there’s a better alternative. During startup, Spring Boot looks for a special environment variable, SPRING_PROFILES_ACTIVE.

尽管如此,还有一个更好的选择。在启动过程中,Spring Boot会寻找一个特殊的环境变量,SPRING_PROFILES_ACTIVE

So, we can practically utilize this with the docker run command to set Spring profiles at startup:

因此,我们实际上可以通过docker run命令来利用这一点在启动时设置Spring配置文件。

docker run -e "SPRING_PROFILES_ACTIVE=test" docker-with-spring-profile:latest

Additionally, depending on our use case, we can set more than one profile at once via comma-separated strings:

此外,根据我们的使用情况,我们可以通过逗号分隔的字符串一次设置多个配置文件。

docker run -e "SPRING_PROFILES_ACTIVE=test1,test2,test3" docker-with-spring-profile:latest

However, we should pay attention that Spring Boot has a particular order between properties. Command-line arguments have precedence over environment variables. For this reason, in order to make SPRING_PROFILES_ACTIVE work, we need to revert our Dockerfile.

但是,我们应该注意,Spring Boot在属性之间有一个特定的顺序命令行参数优先于环境变量。由于这个原因,为了使SPRING_PROFILES_ACTIVE发挥作用,我们需要重新修改我们的Dockerfile

Consequently, we remove the “-Dspring.profiles.active=test” argument from our Dockerfile’s ENTRYPOINT line:

因此,我们从Dockerfile的ENTRYPOINT行中删除“-Dspring.profiles.active=test”参数。

//...
ENTRYPOINT ["java", "-jar", "/app.jar"]

Finally, we can see that the profiles we set via SPRING_PROFILES_ACTIVE are taken into account:

最后,我们可以看到,我们通过SPRING_PROFILES_ACTIVE设置的配置文件被考虑到了。

2022-04-22 22:50:28.924 INFO 1 --- [main] c.b.docker.spring.DemoApplication: Starting DemoApplication using Java 11.0.14.1 on 18eacb6362f8 with PID 1 (/app.jar started by root in /)
2022-04-22T22:50:28.926562249Z 2022-04-22 22:50:28.926 INFO 1 --- [main] c.b.docker.spring.DemoApplication: The following 3 profiles are active: "test1", "test2", "test3"
//..

5. Setting Profile in Docker Compose File

5.在Docker Compose文件中设置配置文件

As an alternative approach, environment variables can also be provided in a docker-compose file.

作为一种替代方法,环境变量也可以在docker-compose文件中提供

Additionally, to utilize our docker run operations in a better way, we can create a docker-compose file for each profile.

此外,为了更好地利用我们的docker run操作,我们可以为每个配置文件创建一个docker-compose文件。

Let’s create a docker-compose-test.yml file for the “test” profile:

让我们为“test”/em>配置文件创建一个docker-compos-test.yml文件。

version: "3.5"
services:
  docker-with-spring-profile:
    image: docker-with-spring-profile:latest
    environment:
      - "SPRING_PROFILES_ACTIVE=test"

Similarly, we create another file, docker-compose-prod.yml, for the “prod” profile — the only difference is the profile “prod” in the second file:

同样,我们为“prod”配置文件创建另一个文件,docker-compos-prod.yml – 唯一的区别是第二个文件中的配置文件“prod”

//...
environment:
  - "SPRING_PROFILES_ACTIVE=prod"

Therefore, we can run our container via two different docker-compose files:

因此,我们可以通过两个不同的docker-compose文件运行我们的容器。

# for the profile 'test'
docker-compose -f docker-compose-test.yml up

# for the profile 'prod'
docker-compose -f docker-compose-prod.yml up

6. Conclusion

6.结语

In this tutorial, we described different ways to set profiles in a dockerized Spring Boot application and also showed some examples both with Docker and Docker Compose.

在本教程中,我们描述了在docker化的Spring Boot应用程序中设置配置文件的不同方法,还展示了一些使用Docker和Docker Compose的例子。

Like always, all the code samples shown in this tutorial are available over on GitHub.

像往常一样,本教程中显示的所有代码样本都可以在GitHub上获得