部署到Docker时外部化Spring Boot属性 [英] Externalising Spring Boot properties when deploying to Docker

查看:146
本文介绍了部署到Docker时外部化Spring Boot属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Spring Boot应用程序中,我想外部化要在Docker容器中运行的属性.首次部署时,my-server/src/main/resources/application.yml中当前存在的属性将按预期方式由应用程序加载和使用.一切正常.

In my Spring Boot app I want to externalise the properties to run in a Docker container. When first deployed, the properties that are currently in my-server/src/main/resources/application.yml are loaded and used by the application as expected. All works fine.

但是,我的问题是我需要根据需要更新这些属性,因此我需要在Docker容器上一次访问application.yml文件.但是在这一点上,它在运行buildDocker任务之前尚未包含在build/docker/目录中,因此在第一次部署后将不会被复制或访问.

However, my problem is that I need these properties to be updatable as needed, so I need access to the application.yml file once on the Docker container. But at this point, it's not included in the build/docker/ directory before running the buildDocker task, so won't be copied over or accessible after first deployment.

因此,我尝试将Yaml文件复制到docker/构建目录中,将其复制到可访问目录(/opt/meanwhileinhell/myapp/conf)中,并使用spring.config.location属性将配置的位置传递给我的Dockerfile中的Jar:

So, what I have tried is to copy the Yaml file into the docker/ build directory, copy it to an accessible directory (/opt/meanwhileinhell/myapp/conf), and use the spring.config.location property to pass a location of the config to the Jar in my Dockerfile:

ENTRYPOINT  ["java",\
...
"-jar", "/app.jar",\
"--spring.config.location=classpath:${configDirectory}"]

查看在Docker容器上运行的命令,我可以看到这与预期的一样:

Looking at the Command running on the Docker container I can see that this is as expected:

/app.jar --spring.config.location=classpath:/opt/meanwhileinhell/myapp/conf]

但是,当我更新此文件中的属性并重新启动Docker容器时,它并没有接受更改.文件权限为:

However, when I update a property in this file and restart the Docker container, it isn't picking up the changes. File permissions are:

-rw-r--r-- 1 root root  618 Sep  5 13:59 application.yml

文档状态:

配置了自定义配置位置后,还会使用它们 到默认位置.在搜索自定义位置之前, 默认位置.

When custom config locations are configured, they are used in addition to the default locations. Custom locations are searched before the default locations.

我似乎无法弄清楚我做错了什么或曲解了,但更重要的是,这是将这种Docker场景的配置外部化的正确方法吗?

I can't seem to figure out what I'm doing wrong or misinterpreting, but probably more importantly, is this the correct way to externalise the config for this type of Docker scenario?

推荐答案

DOCKER图像配置

如果您希望 Spring推荐的方式来启动Spring Boot动力的Docker容器,这就是您所发现的:

If you look to the way Spring recommends to launch a Spring Boot powered docker container, that's what you find:

FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

这意味着您的映像扩展了openjdk,并且容器具有自己的环境.如果这样做的话,只需声明要覆盖的内容作为环境属性就足够了,因为

That means your image extends openjdk and your container has its own environment. If you're doing like that, it would be enough to declare what you want to override as environment properties and Spring Boot will fetch them, since environment variables take precedence over the yml files.

环境变量也可以在docker命令中传递,以使用所需的配置启动容器.如果要为JVM内存设置一些限制,请参见下面的链接.

Environment variables can be passed in your docker command too, to launch the container with your desired configuration. If you want to set some limit for the JVM memory, see the link below.

DOCKER组成样本

这里有一个示例,说明了如何使用docker compose启动一个简单的应用程序环境.如您所见,我在这里将spring.datasource.url属性声明为环境变量,因此它会覆盖您在application.yml文件中获得的所有内容.

Here you have an example of how I launch a simple app environment with docker compose. As you see, I declare the spring.datasource.url property here as an environment variable, so it overrides whatever you've got in your application.yml file.

version: '2'
services:
    myapp:
        image: mycompany/myapp:1.0.0
        container_name: myapp
        depends_on:
        - mysql
        environment:
            - SPRING_DATASOURCE_URL=jdbc:mysql://mysql:3306/myapp?useUnicode=true&characterEncoding=utf8&useSSL=false
        ports:
            - 8080:8080

    mysql:
        image: mysql:5.7.19
        container_name: mysql
        volumes:
            - /home/docker/volumes/myapp/mysql/:/var/lib/mysql/
        environment:
            - MYSQL_USER=root
            - MYSQL_ALLOW_EMPTY_PASSWORD=yes
            - MYSQL_DATABASE=myapp
        command: mysqld --lower_case_table_names=1 --skip-ssl --character_set_server=utf8

另请参见:

这篇关于部署到Docker时外部化Spring Boot属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆