Bobcares

Set Dynamic Environment Variables for Docker multi-stage builds

by | Aug 29, 2021

To set Dynamic Environment Variables for Docker multi-stage builds, we can use an SSM parameter or an Elastic Beanstalk variable.

It’s a best practice to use an SSM parameter because we require to set the variable one time only.

As part of our Docker Hosting Services, we assist our customers with several Docker queries.

Today, let us see how we can set Dynamic Environment Variables.

 

Set Dynamic Environment Variables for Docker multi-stage builds

Moving ahead, let us see how our Support Techs perform this query.

 

  • Use an SSM parameter

To allow the instance to retrieve the SSM parameter, we confirm that the instance profile has the ssm:GetParameters permission.

1. Initially, we create an SSM parameter.

aws ssm put-parameter --name PRODURL_SSM --value http://myproddomain.com --type String

2. Then we deploy the application using the following Dockerfile.

Generally, the Dockerfile retrieves the SSM parameter value inside the temporary/build container. Later, during the build, it exports the variable.

# Build environment
FROM node:13.12.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm install react-scripts@3.4.1
COPY . ./
## Install the required packages for awscli, curl, and jq. ##
RUN apk -v --no-cache add \
py-pip \
curl \
jq \
&& \
pip install awscli --upgrade --user
## Export the region dynamically retrieving it from the instance metadata, and then retrieve the SSM parameter value using awscli. Then, place the parameter in a text file, and export the variable using the value from the text file. Finally, run the build. ##
RUN export REGION=$(curl -s http://xxx.xxx.xxx.xxx/latest/dynamic/instance-identity/document | jq -r .region) && /root/.local/bin/aws ssm get-parameters --names PRODURL_SSM --region $REGION | grep Value | cut -d '"' -f4 >> /tmp/SSMParameter.txt && export DYNAMIC_SSM_VAR=$(cat /tmp/SSMParameter.txt) && npm run build
# Production environment
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
## Copy the text file that has the SSM parameter value from the build container to the production container to confirm the variable was retrieved successfully. ##
COPY --from=build /tmp/SSMParameter.txt /tmp
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

3. Once done, we can check if the deployment was a success.

To do so, we check the file we copy from the build container. This file should include the value of the SSM parameter variable.

# docker exec -it <Container-Id> sh
# cat /tmp/SSMParameter.txt
http://myproddomain.com

 

  • Use an Elastic Beanstalk variable

1. First, we copy the config.yml file to the project root directory.

cp .elasticbeanstalk/config.yml

2. Then to set the Elastic Beanstalk variable with the EB CLI and to update or deploy to an existing environment, we run:

eb setenv PRODURL_EB=http://myproddomain.com

3. We add the new environment name in the config.yml file in the project root directory to create a new Elastic Beanstalk environment, and run:

eb create --envvars PRODURL_EB=http://myproddomain.com

4. After that, we deploy the application using the following Dockerfile.

The Dockerfile retrieves the value of the Elastic Beanstalk variable inside the build container and then exports the variable during the build.

# Build environment
FROM node:13.12.0-alpine as build
WORKDIR /app
ENV PATH /app/node_modules/.bin:$PATH
COPY package.json ./
COPY package-lock.json ./
RUN npm install react-scripts@3.4.1
COPY . ./
## Create an .elasticbeanstalk directory to place the config.yml file in, so that the eb cli can interact with the Elastic Beanstalk environment. ##
RUN mkdir .elasticbeanstalk
## Copy config.yml to /app/.elasticbeanstalk inside the build container as it will be used by eb cli ##
COPY config.yml /app/.elasticbeanstalk
## Install required packages for awsebcli ##
RUN apk -v --no-cache add \
gcc \
musl-dev \
openssl \
openssl-dev \
make \
py-pip \
libffi-dev \
python \
python-dev \
&& \
pip install awsebcli --upgrade --user 
## Retrieve the Elastic Beanstalk variable using awsebcli and place it in a text file. Then, export the desired variable using the value from the text file, then run the build. ##
RUN /root/.local/bin/eb printenv | grep PRODURL_EB | awk '{print $3}' >> /tmp/EBVar.txt && export DYNAMIC_EB_VAR=$(cat /tmp/EBVar.txt) && npm run build
# Production environment
FROM nginx:stable-alpine
COPY --from=build /app/build /usr/share/nginx/html
## Copy the text file that has the Elastic Beanstalk variable value from the build container to the production container to confirm the variable was retrieved successfully ##
COPY --from=build /tmp/EBVar.txt /tmp
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]

5. To check the success of the deployment, we check the file from the build container.

# docker exec -it <Container-Id> sh
# cat /tmp/EBVar.txt 
http://myproddomain.com

[Stuck with the procedures? We’d be happy to assist]

 

Conclusion

In short, we saw how our Support Techs set up dynamic environment variables.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF