How to pass environment variables while deploying docker container to kubernetes

Hello,

We have Azure Pipeline setup to build docker image and store to container registry in Azure. We have configured the environment variables (development, staging, and production) in our application code.

Now we have to use the Build Once, Deploy everywhere (development, staging, and production) strategy. We are using azuredeployment.yaml ( below) to deploy the docker image to Kubernetes.

apiVersion: apps/v1
kind: Deployment
metadata:
name: application-1-deployment
labels:
app: application-1
spec:
replicas: 2
selector:
matchLabels:
app: application-1
template:
metadata:
labels:
app: application-1
spec:
containers:
- name: application-1
image: application-1.azurecr.io/user-stub-service:v#{Build.BuildId}#
ports:
- containerPort: 5001

# start - service

apiVersion: v1
kind: Service
metadata:
name: application-1
spec:
selector:
app: application-1
ports:
- protocol: TCP
port: 5001
targetPort: 5001
type: LoadBalancer

How can we configure the pipeline and kubectl command to pass the environment variables?

Hello, @mrugeshah
I think this link is suitable for your query.

I think my question is not perfect. Starting from scratch.

We have aspnet core 2.1 application and we are using Azure Pipeline to build and release the application.

We are using docker build to build image in the build pipeline and deploy the application to Kubernetes in the release pipeline.

Now we would like to build the image once and deploy the image to Kubernetes in 3 stages (Development, Staging, and Production).

We are using ASPNETCORE_ENVIRONMENT environment variable to differentiate the stages. We tried to use this variable in DockerFile and Kubernetes deployment.azure.yaml file. Also, We have configured Docker Profile in launchsettings.json in the application.

“Docker”: {
“commandName”: “Docker”,
“environmentVariables”: {
“ASPNETCORE_ENVIRONMENT”: “”,
“ASPNETCORE_URLS”: “”,
“DOTNET_ENVIRONMENT”: “Staging”
}
},

But still, the environment variables are not working as expected. Every time we deploy the application to Kubernetes It is taking the Development variable.

Please suggest a better plan or changes achieve “Build Once, Deploy Everywhere” strategy.

Thanks,
Mrugesh