A better way of doing "with-docker-mulit-env" #46760
Unanswered
haraldev01
asked this question in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I needed to have different environment variables for deployment, testing and development, as you most often need.
I had a pipeline set up with AWS where the image would be built, then it runs on AWS. However, the environment variables that I gave to AWS weren't made available inside the running container with the NextJS app. I found out this is by design although I do not understand why.
I quickly understood that I needed to "bake" the environment variables at build time.
So I made two identical pipelines, just that they push to different registries (one for staging, one for prod), and they have slightly different environment variables. So the one for the staging environment has "NODE_ENV=staging", and the one for prod has "NODE_ENV=production". These values are passed into the DOCKER BUILD command with
--build-arg NODE_ENV=$NODE_ENV
Then in the Dockerfile, it will copy files from one of multiple .env* files depending on what the NODE_ENV argument is:
COPY .env.${NODE_ENV}.sample .env.production
I find this to be much more efficient than having three practically identical dockerfiles and then fussing over which one to build from. Just use:
docker build -t my:tag --build-arg NODE_ENV=$NODE_ENV .
where
$NODE_ENV
is the build time environment variable. So this above snippet is from the buildspec.yaml file that AWS CodeBuild uses.Beta Was this translation helpful? Give feedback.
All reactions