覆盖 Docker 容器中的 Spring Boot 属性

huangapple go评论69阅读模式
英文:

Override Spring boot properties in docker container

问题

我有一个使用了 Spring Boot 的应用程序,已经被 Docker 化了。默认情况下,该应用程序的 spring.cloud.config.enabled 属性被设置为 false,因此该应用程序不会从配置服务器(configserver)中获取 application.properties 的配置。然而,在部署到另一个环境时,我们需要将应用程序与配置服务器集成,所以需要覆盖并将 spring.cloud.config.enabled 属性设置为 true

为了实现这一点,我使用了以下的 Docker Compose 文件来运行 Docker 镜像:

version: '3'

services:
  my-app-new:
    container_name: my-app
    image: my-app:1.0.0-SNAPSHOT
    ports:
      - "8070:8070"
    environment:
      - SPRING_CLOUD_CONFIG_ENABLED=true
      - SPRING_CLOUD_CONFIG_URI=http://localhost:14180

然而,这样并没有起作用。如果我在属性文件中直接硬编码这些值,那么集成是正常的。

我还尝试了以下命令,但仍然没有起作用:

docker run -p 8070:8070 -e SPRING_CLOUD_CONFIG_ENABLED=true -e SPRING_CLOUD_CONFIG_URI=http://localhost:14180 my-app:1.0.0-SNAPSHOT

Spring Boot 版本为 2.2.6。

请告诉我问题出在哪里。

更新:
由于我们公司有太多的环境,而且甚至虚拟机也在不断变化,所以我不能使用配置文件。我们希望有一种可以从外部传递某些变量的解决方案。

正如某人在评论中指出的那样,上述的 Compose YML 文件不起作用,因为环境变量需要被 Spring Boot 应用程序读取。因此,在互联网上进行了一些研究,我们现在是在运行镜像时传递 JAVA_OPTS Tomcat 变量。像这样:

docker run --env JAVA_OPTS="-Dspring.cloud.config.uri=http://localhost:14180 -Dspring.cloud.config.enabled=true" -p 8080:8080 my-app-image

在 Docker 文件中,我们在启动 jar 文件时使用了 JAVA_OPTS,像这样:

ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar app.jar" ]

然而,这仍然没有起作用。不太清楚哪里出了问题。

英文:

I have a spring boot application that is dockerized. By default the application has spring.cloud.config.enabled=false hence the application doesn't pick up the application.properties from configserver. However while deploying in a separate env we need to integrate the application with configserver so we need to override and set the spring.cloud.config.enabled property to true.

To achieve this I am running the docker image using the following docker-compose file :

version: '3'

services:
  my-app-new:
    container_name: my-app
    image: my-app:1.0.0-SNAPSHOT
    ports:
      - "8070:8070"
    environment:
      - SPRING_CLOUD_CONFIG_ENABLED=true
      - SPRING_CLOUD_CONFIG_URI=http://localhost:14180

However, it just doesn't work. If I hard code the values in the property file then it integrates fine.

I also tried the following command but it still didn't work :

docker run -p 8070:8070 -e SPRING_CLOUD_CONFIG_ENABLED=true -e SPRING_CLOUD_CONFIG_URI=http://localhost:14180 my-app:1.0.0-SNAPSHOT

The spring boot version is 2.2.6.

Let me know what the problem is.

Update :
I cannot use profiles as there too many env in our company and even the VMs keep getting changed so cannot have hardcoded profiles. We want a solution where we can just pass certain variables from the outside.

As someone pointed out in the comments the above compose yml is not working as the environment variables need to read by the spring boot application. So did some research on the internet and instead we are now passing the JAVA_OPTS tomcat variable while running the image. Like so :

docker run --env JAVA_OPTS="-Dspring.cloud.config.uri=http://localhost:14180 -Dspring.cloud.config.enabled=true" -p 8080:8080 my-app-image

And in the docker file we have used the JAVA_OPTS while starting the jar like so

ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -jar app.jar" ]

And this still doesnt work. Not sure what is going wrong.

答案1

得分: 1

我找到了我的设置问题。我犯了一个愚蠢的错误。配置服务器不在我的Docker网络中,而我使用localhost与配置服务器通信。当然,localhost指的是我正在引用的应用程序容器IP,该容器只运行了应用程序。相反,当我使用我的机器的IP地址或主机名时,我的应用程序容器可以成功连接到配置服务器。

英文:

I found the problem with my setup. I made a silly error. The config server is not in my docker network and I used localhost to communicate with the config server. Localhost would of course mean that I am referring to the app containers IP which only has the app running. Instead when I used the ip address or the hostname of my machine my application container could connect to the config server successfully.

答案2

得分: -2

为什么你不运行容器,进入其中,修改配置,然后提交为新镜像。
之后部署到新环境。

英文:

Why you not run container --> go inside --> change configuration and commit to new images.
After that deploy to new env.

huangapple
  • 本文由 发表于 2020年9月4日 12:31:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63734870.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定