<p>I’m trying to set some environment variables in k8s deployment and use them within the <code>application.properties</code> in my spring boot application, but it looks like I'm doing something wrong because spring is not reading those variables, although when checking the <code>env vars</code> on the pod, all the vars are set correctly.</p>
<p>The error log from the container:</p>
<pre><code>org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port...
</code></pre>
<p>Any help will be appreciated.</p>
<p><strong>application.properties:</strong></p>
<pre><code>spring.datasource.url=jdbc:postgresql://${DB_URL}:${DB_PORT}/${DB_NAME}
spring.datasource.username=${DB_USER_NAME}
spring.datasource.password=${DB_PASSWORD}
</code></pre>
<p><strong>DockerFile</strong></p>
<pre><code>FROM openjdk:11-jre-slim-buster
ARG JAR_FILE=target/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT [&quot;java&quot;,&quot;-jar&quot;,&quot;/app.jar&quot;]
</code></pre>
<p><strong>deployment.yaml:</strong></p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: api-deployment
  labels:
    app: api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: api
  template:
    metadata:
      labels:
        app: api
    spec:
      containers:
        - name: api
          image: .../api
          resources:
            requests:
              memory: &quot;64Mi&quot;
              cpu: &quot;250m&quot;
            limits:
              memory: &quot;128Mi&quot;
              cpu: &quot;500m&quot;
          ports:
            - containerPort: 80
          env:
          - name: DB_URL
            value: &quot;posgres&quot;
          - name: DB_NAME
            valueFrom:
              configMapKeyRef:
                name: postgres-config
                key: dbName
          - name: DB_USER_NAME
            valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: dbUserName
          - name: DB_PASSWORD
            valueFrom:
                secretKeyRef:
                  name: db-secret
                  key: dbPassword
</code></pre>

<p>The DockerFile was wrong.
Everything is working fine after changing the DockerFile to this:</p>
<pre><code>FROM maven:3.6.3-openjdk-11-slim as builder

WORKDIR /app
COPY pom.xml .

COPY src/ /app/src/
RUN mvn install -DskipTests=true

FROM adoptopenjdk/openjdk11:jre-11.0.8_10-alpine

COPY --from=builder /app/target/*.jar /app.jar

ENTRYPOINT [&quot;java&quot;, &quot;-jar&quot;, &quot;/app.jar&quot;]
</code></pre>
