<p>I have a problem where in my dockerized Spring Boot application is not using the <code>application.properties</code> I stored in a <code>configMap</code>.</p>

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

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

<p>It seems that the Spring Boot uses the first <code>application.properties</code> and when k8s overwrites it, apparently, it doesn't use it.</p>

<p>It seems that, apparently, what happens is:</p>

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

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

<pre><code>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"]
</code></pre>

<p>Here's my Kubernetes deployment .yaml file for reference:</p>

<pre><code>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
</code></pre>

<p>And my configMap for reference:</p>

<pre><code>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
</code></pre>

<p>Any help is greatly appreciated... Thank you so much.. :)</p>

Ken Flake
<p>The thing is that <code>/src/main/resources/application.properties</code> that your application uses is the one that is inside the jar file by default. If you open your jar, you should see it there.
That being said, your expectations to mount a <code>/src/main/resources</code> directory where your jar is are not going to be fulfilled, unfortunately.
<a href="https://docs.spring.io/spring-boot/docs/1.0.1.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files" rel="nofollow noreferrer">These are the docs you should be looking at</a>. </p>

<p>I won't go into much detail as it's explained pretty good in the docs but I will say that you are better off explicitly declaring your config location so that new people on the project know from where the config is coming from right off the bat. </p>

<p>You can do something like this:</p>

<pre><code>---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-project-api
  labels:
    app: my-project-api
spec:
  selector:
    matchLabels:
      app: my-project-api
  template:
    metadata:
      labels:
        app: my-project-api
    spec:
      containers:
        - name: my-project-api
          image: "my-project:latest"
          imagePullPolicy: Always
          env:
            - name: JAVA_OPTS
              value: "-Dspring.config.location=/opt/config"
            .
            .
            .
          volumeMounts:
            - name: my-project-config
              mountPath: /opt/config
          ports:
            - containerPort: 8080
      volumes:
        - name: my-project-config
          configMap:
            name: my-project-config
</code></pre>

<p>Hope that helps,</p>

<p>Cheers!</p>
