<p>i am very new to Spring Boot and the application.properties. I have the problem, that i need to be very flexible with my database port, because i have two different databases. Therefore i want to read the port from a environment variable. I tried the following:</p>

<pre><code>spring.data.mongodb.uri = mongodb://project1:${db-password}@abc:12345/project

</code></pre>

<p>This code works fine, if my Database has the port 12345. But if i now try to read the port from an environment variable there is a problem.
I tried this:</p>

<pre><code>spring.data.mongodb.uri = mongodb://project1:${db-password}@abc:${port}/project

</code></pre>

<p>The problem is the following: I am using k8 and Jenkins. The environment variable "port" is given to my program in my k8 and this works fine for "db-password", but not for the port. My Jenkins says:
"The connection string contains an invalid host 'abd:${port}'. The port '${port}' is not a valid, it must be an integer between 0 and 65535"</p>

<p>So now to my question:
How can i read a port as an environment variable, without getting this error?</p>

<p>Thank you in advance!</p>

slaayaah
<p>To inject environment variable to the pods you can do the following: </p>

<h3>Configmap</h3>

<p>You can create <code>ConfigMap</code> and configure your pods to use it. </p>

<p>Steps required:</p>

<ul>
<li>Create <code>ConfigMap</code></li>
<li>Update/Create the deployment with ConfigMap</li>
<li>Test it</li>
</ul>

<h3>Create ConfigMap</h3>

<p>I provided simple <code>ConfigMap</code> below to store your variables:</p>

<pre><code>apiVersion: v1
kind: ConfigMap
metadata:
  name: example-config
data:
  port: "12345"
</code></pre>

<p>To apply it and be able to use it invoke following command: </p>

<p><code>$ kubectl create -f example-configmap.yaml</code></p>

<p>The <code>ConfigMap</code> above will create the environment variable <code>port</code> with value of <code>12345</code>. </p>

<p>Check if <code>ConfigMap</code> was created successfully: </p>

<p><code>$ kubectl get configmap</code></p>

<p>Output should be like this: </p>

<pre class="lang-sh prettyprint-override"><code>NAME             DATA   AGE
example-config   1      21m
</code></pre>

<p>To get the detailed information you can check it with command:</p>

<p><code>$ kubectl describe configmap example-config</code></p>

<p>With output:</p>

<pre><code>Name:         example-config
Namespace:    default
Labels:       &lt;none&gt;
Annotations:  &lt;none&gt;
Data
====
port:
----
12345
Events:  &lt;none&gt;
</code></pre>

<h3>Update/Create the deployment with ConfigMap</h3>

<p>I provided simple deployment with <code>ConfigMap</code> included: </p>

<pre><code>apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9
        envFrom:
          - configMapRef:
              name: example-config
        ports:
        - containerPort: 80
</code></pre>

<p>Configuration responsible for using <code>ConfigMap</code>: </p>

<pre><code>        envFrom:
          - configMapRef:
              name: example-config
</code></pre>

<p>After that you need to run your deployment with command: </p>

<p><code>$ kubectl create -f configmap-test.yaml</code></p>

<p>And check if it's working: </p>

<p><code>$ kubectl get pods</code></p>

<p>With output: </p>

<pre><code>NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-84d6f58895-b4zvz   1/1     Running   0          23m
nginx-deployment-84d6f58895-dp4c7   1/1     Running   0          23m
</code></pre>

<h3>Test it</h3>

<p>To test if environment variable is working you need to get inside the pod and check for yourself.</p>

<p>To do that invoke the command:</p>

<p><code>$ kubectl exec -it NAME_OF_POD -- /bin/bash</code></p>

<p>Please provide the variable NAME_OF_POD with appropriate one for your case. </p>

<p>After successfully getting into container run: </p>

<p><code>$ echo $port</code></p>

<p>It should show: </p>

<pre class="lang-sh prettyprint-override"><code>root@nginx-deployment-84d6f58895-b4zvz:/# echo $port
12345
</code></pre>

<p>Now you can use your environment variables inside pods. </p>
