Externalising Spring Boot properties when deploying to Docker

MeanwhileInHell :

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.

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.

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}"]

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]

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

The documentation states:

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

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?

Xtreme Biker :

DOCKER IMAGE CONFIGURATION

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"]

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.

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 COMPOSE SAMPLE

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

See also:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Overriding spring-boot application properties when deploying in JBoss

Spring Boot - Where to put properties files when deploying to Tomcat?

CORS errors when deploying Spring Boot and Angular app in Docker

How to externalise .properties files when deploying a Spring Boot WAR in Wildfly Server?

SQLSyntaxErrorException when deploying Spring Boot app to Heroku

NoSuchMethodError when deploying Spring Boot to Weblogic

Deploying Spring boot App, to Heroku With Docker using github

How to containerize the spring boot application without installing docker and deploying in centos

Pass Docker arguments to Spring boot properties

Spring boot deploying a war

How to specify a profile when deploying a Spring boot war file to Tomcat?

502 Bad Gateway when deploying Spring Boot application to Elastic Beanstalk

Unable to resolve view exception with Spring Boot WebFlux when deploying as WAR

Mapping conflict when deploying with spring boot Jersey starter deployed as WAR

Deploying spring boot fat jar

Deploying Spring Boot Application on Jenkins

Problem with deploying spring boot application

Spring Boot External Configuration when using Docker

Docker error when containerizing a spring boot app

Why is ! in the properties file path in spring-boot application on the docker?

How to read spring boot application properties from a different docker container?

Spring boot - Default properties file not being read when using profile

Spring Boot error when I use a properties value inside an enum

Spring Boot: Datasource properties

refreshing spring boot properties

java.lang.NoSuchMethodException when deploying spring-boot app to cloud foundry

No bean named 'loggingFilter' available error when deploying Spring Boot application in Tomcat

How to solve NoSuchMethodError for javax.validation when deploying a spring boot application in weblogic server?

Error creating bean with name 'undertowServletWebServerFactory' when deploying Spring Boot application in JBoss EAP 7.1.0