External Spring Docker Deployment Boot Properties

In my Spring Boot application, I want to output properties to run in a Docker container. When deployed for the first time, properties that are currently in my-server/src/main/resources/application.ymlare loaded and used by the application, as expected. Everything works great.

However, my problem is that I need these properties to be updated as needed, so I need to access the file application.ymlonce in the Docker container. But at the moment it is not included in the directory build/docker/before starting the task buildDocker, therefore it will not be copied or available after the first deployment.

So, I tried to copy the Yaml file to the assembly directory docker/, copy it to an accessible directory ( /opt/meanwhileinhell/myapp/conf), and use the property spring.config.locationto pass the configuration location in the Jar to my Dockerfile:

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

Looking at the command running in the Docker container, I 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 does not commit the changes. File Permissions:

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

The documentation documentation says:

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

, , , , , , Docker ?

+20
6

DOCKER

, Spring - Spring Boot, :

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, . , , , , Spring Boot , yml..

docker, . JVM, . .


, docker compose. , spring.datasource.url , , application.yml.

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

:

+26

Spring Cloud Config Server , .

tl; dr git ( , ..) / , REST. Spring Boot ; , .

https://spring.io/guides/gs/centralized-configuration/

+6

Xtreme Biker answer, w860 > TomCat...

application.yml , Docker , .

( Docker):

  • ,
  • ; - , -,
  • ;

Spring :

  • UN * X env vars (.. SPRING_DATASOURCE_USERNAME=helloworld)
  • Java (.. -Dspring.datasource.username=helloworld)

Java, : " Java Java-".

: TomCat CATALINA_OPTS Java. catalina.sh:

() Java, , "run" "debug". , JAVA_OPTS , Tomcat, , .. , GC, JMX ..

CATALINA_OPTS - , Docker, setenv.sh, enk Docker.


.war :

./gradlew war

, .war Gradle build/libs/api-0.0.1-SNAPSHOT.war.

Docker:

FROM tomcat:8.5.16-jre8-alpine

EXPOSE 8080

COPY build/libs/api-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/v1.war

CMD ["catalina.sh", "run"]

Docker :

docker build . --tag=my-api

CATALINA_OPTS :

docker run -it \
-p 8080:8080 \
-e CATALINA_OPTS="\
-Dspring.datasource.url='jdbc:mysql://mydatabase.stackoverflow.com:3306' \
-Dspring.datasource.username=myuser \
" \
my-api

:

version: '3.2'
services:
  web:
    image: my-api
    ports:
      - "8080:8080"
    environment:
      - >
        CATALINA_OPTS=
        -Dspring.datasource.url='jdbc:mysql://mydatabase.stackoverflow.com:3306'
        -Dspring.datasource.username=myuser
+4

. , DockerFile:

"--spring.config.location=classpath:${configDirectory}"]

:

 "--spring.config.location=file:${configDirectory}/application.yml"]

Docker.

+3

, , . , .

spring , . Spring Cloud Config. , , git , . application.yml git .

Following this approach, you can define multiple configuration files for different environments and keep the docker container unchanged.

+2
source

I would personally consider two options:

  1. Using an environment variable

    app:
      image: my-app:latest
      ports:
        - "8080:8080"
      environment:
         SPRING_DATASOURCE_URL=jdbc:mysql://db:3306/table
    
  2. Using SPRING_APPLICATION_JSON

    app:
      image: my-app:latests
      ports:
        - "8080:8080"
      environment:
        SPRING_APPLICATION_JSON: '{
          "spring.datasource.url": "jdbc:mysql://db:3306/table",
        }'
    
+1
source

All Articles