<p>Assume there is a backend application with a private key stored in a .env file.</p>
<p>For the project file structure:</p>
<pre><code>|-App files
|-Dockerfile
|-.env
</code></pre>
<p>If I run the docker image locally, the application can be reached normally by using a valid public key during the API request. However, if I deploy the container into AKS cluster by using same docker image, the application failed.</p>
<p>I am wondering how the container in a AKS cluster handle the .env file. What should I do to solve this problem?</p>

Yuk Chan
<p>Moving this out of comments for better visibility.</p>
<hr />
<p>First and most important is docker is not the same as kubernetes. What works on docker, won't work directly on kubernetes. Docker is a container runtime, while kubernetes is a container orchestration tool which sits on top of docker (not always docker now, <code>containerd</code> is used as well).</p>
<p>There are many resources on the internet which describe the key difference. For example this one is from <a href="https://azure.microsoft.com/en-us/topic/kubernetes-vs-docker/" rel="nofollow noreferrer">microsoft docs</a></p>
<hr />
<p>First <code>configmaps</code> and <code>secrets</code> should be created:</p>
<p><a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/#create-a-configmap" rel="nofollow noreferrer">Creating and managing configmaps</a> and <a href="https://kubernetes.io/docs/concepts/configuration/secret/#creating-a-secret" rel="nofollow noreferrer">creating and managing secrets</a></p>
<p>There are different types of <a href="https://kubernetes.io/docs/concepts/configuration/secret/#secret-types" rel="nofollow noreferrer">secrets</a> which can be created.</p>
<hr />
<ol>
<li>Use configmaps/secrets as environment variables.</li>
</ol>
<p>Further referring to <code>configMaps</code> and <code>secrets</code> as <code>environment variables</code> looks like (configmaps and secrets have the same structure):</p>
<pre><code>apiVersion: v1
kind: Pod
metadata: 
  name: pod-example
spec: 
  containers: 
    - ...
      env: 
        - 
          name: ADMIN_PASS
          valueFrom: 
            secretKeyRef: # here secretref is used for sensitive data
              key: admin
              name: admin-password
        - 
          name: MYSQL_DB_STRING
          valueFrom: 
            configMapKeyRef: # this is not sensitive data so can be used configmap
              key: db_config
              name: connection_string
      ...
</code></pre>
<ol start="2">
<li>Use configmaps/secrets as volumes (it will be presented as file).</li>
</ol>
<p>Below the example of using secrets as files mounted in a specific directory:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
...
    spec:
      containers:
      - ...
        volumeMounts:
        - name: secrets-files
          mountPath: &quot;/mnt/secret.file1&quot;  # &quot;secret.file1&quot; file will be created in &quot;/mnt&quot; directory
          subPath: secret.file1
      volumes:
        - name: secrets-files
          secret:
            secretName: my-secret # name of the Secret
</code></pre>
<p>There's a <a href="https://medium.com/avmconsulting-blog/secrets-management-in-kubernetes-378cbf8171d0" rel="nofollow noreferrer">good article</a> which explains and shows use cases of secrets as well as its limitations e.g. size is limited to 1Mb.</p>
