Reputation: 881
My requirement is to set few environment variables on the docker image that is built by azure devops pipeline.
This pipeline is used for dev/stage/production environments, each stage is tied to its own set of group variables, these variables I want to be available in nodejs app.
I can read the variables within the pipeline using $VARNAME but cant read same when the code runs using process.env.VARNAME.
I understand that it is not the best approach as the image would have environment variables which may potentially have secrets hence I am open to different ideas as well.
What I have tried so far: Added ARG in dockerfile ARG VARNAME=somevalue on the docker build task added
- task: Docker@2
displayName: Build docker image
inputs:
command: build
repository: $(imageName)
tags: $(Build.BuildId)
buildContext: '$(Pipeline.Workspace)/PublishedWebApp'
arguments: --build-arg SOMEVAR=anewvalue
I try to access this as process.env.SOMEVAR in nodejs code.
I can actually see the --build-arg on the docker build executed in the pipeline but in the code it never appears.
What I am after is pretty standard requirement, we have multiple environments, each environment will have different keys (different group variables tied to different stage), how do I pass different keys to the deployment?
Upvotes: 5
Views: 4232
Reputation: 173
There are a few components from what I can see that need to align:
--build-arg
. This is correct.ARG
in the dockerfile to receive what is passed in via --build-arg
. This is correct.FROM python:3.7-slim
ARG SOME_PASSED_IN_ARG
ENV SOMEVARNAME $SOME_PASSED_IN_ARG
...
You would access SOMEVARNAME
like process.env.SOMEVARNAME
from the node application.
I give credit to @levi-lu-msft in their post here: https://stackoverflow.com/a/59784488/1490277
As for best practice, defining logic default environment variables in the container at BUILD is perfectly acceptable (e.g. ENV=PRODUCTION). At RUN, you are able to to overwrite any environment variable using passed in arguments:
docker run ... --e SOME_PASSED_IN_ARG=someOtherValue
A few "better" practices:
Upvotes: 2