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.