Dockerized Spring Boot app not using mounted Kubernetes ConfigMap (application.properties)

I have a problem where in my dockerized Spring Boot application is not using the application.properties I stored in a configMap.

However, I can see and confirm that my configMap has been mounted properly in the right directory of my Spring Boot app when I enter the pod’s shell.

Note that I have an application.properties by default wherein Kubernetes mounts / overwrites it later on.

It seems that the Spring Boot uses the first application.properties and when k8s overwrites it, apparently, it doesn’t use it.

It seems that, apparently, what happens is:

  • run the .jar file inside the Dockerized Spring Boot app
  • use the first/default application.properties file on runtime
  • Kubernetes proceeds to mount the configMap
  • mount / overwrite success, but how will Spring Boot use this one since it’s already running?

Here is the Dockerfile of my Spring Boot / Docker image for reference:

FROM maven:3.5.4-jdk-8-alpine

# Copy whole source code to the docker image
# Note of .dockerignore, this ensures that folders such as `target` is not copied
WORKDIR /usr/src/myproject
COPY . /usr/src/myproject/

RUN mvn clean package -DskipTests

WORKDIR /usr/src/my-project-app
RUN cp /usr/src/myproject/target/*.jar ./my-project-app.jar
EXPOSE 8080
CMD ["java", "-jar", "my-project-app.jar"]

Here’s my Kubernetes deployment .yaml file for reference:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-project-api
  namespace: my-cluster
  labels:
    app: my-project-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-project-api
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
  template:
    metadata:
      labels:
        app: my-project-api
    spec:
      containers:
        - name: my-project-api
          image: "my-project:latest"
          imagePullPolicy: Always
          env:
            .
            .
            .
          volumeMounts:
          - name: my-project-config
            mountPath: /usr/src/my-project/my-project-service/src/main/resources/config/application.properties
          ports:
            - containerPort: 8080
              name: my-project-api
              protocol: TCP
      volumes:
        # Name of the volume
        - name: my-project-config
          # Get a ConfigMap with this name and attach to this volume
          configMap:
            name: my-project-config

And my configMap for reference:

kind: ConfigMap
apiVersion: v1
data:
  application.properties: |-
    # This comment means that this is coming from k8s ConfigMap. Nice!
    server.port=8999
    .
    .
    .
    .

metadata:
  name: my-project-config
  namespace: my-cluster

Any help is greatly appreciated… Thank you so much.. 🙂

Source: StackOverflow